0

Taking the first N number of rows is pretty straightforward and works well with

var topPvtTbl = new TopPivotTable(runningPvtTbl, 3, 99);
                                topPvtTbl.IncludeOtherGroups = false;

Is there an inverse? My use case is that the data is returning dates in ascending order on the rows and instead of taking the first 3 dates, I want the last 3 dates. I don't see a BottomPivotTable option.

Vitaliy Fedorchenko
  • 8,447
  • 3
  • 37
  • 34
Jason
  • 13
  • 3

1 Answers1

0

You can achieve what you want in the following way:

  • configure base PivotTable class to order axis with dates in reverse order (descending)
  • apply TopPivotTable wrapper to select only first 3 dates (= so they will be actually last 3 days)
  • reverse axis keys with Array.Reverse. For example, if dates are columns: Array.Reverse(topPvtTbl.ColumnKeys);

-- update --

This approach will not work if underlying table-related calculation relies on the concrete axis order (like RunningValuePivotTable). Alternatively, you can implement your own IPivotTable wrapper that takes last N items. For example:

public class LastNColumnsPivotTable : IPivotTable {
  IPivotTable basePvtTbl;

  public LastNColumnsPivotTable(IPivotTable pvtTbl, int lastN) {
    basePvtTbl = pvtTbl;
    if (basePvtTbl.ColumnKeys.Length>lastN) {
      lastColumnKeys = basePvtTbl.ColumnKeys.Skip(basePvtTbl.ColumnKeys.Length - lastN).ToArray();
    } else {
      lastColumnKeys = basePvtTbl.ColumnKeys;
    }
  }

  public string[] Columns { get { return basePvtTbl.Columns; } }
  public string[] Rows { get { return basePvtTbl.Rows; } }

  ValueKey[] lastColumnKeys;
  public ValueKey[] ColumnKeys { get { return lastColumnKeys; } }

  public ValueKey[] RowKeys { get { return basePvtTbl.RowKeys; } }
  public IPivotData PivotData { get { return basePvtTbl.PivotData; } }

  public IAggregator GetValue(ValueKey rowKey, ValueKey colKey) {
    return basePvtTbl.GetValue(rowKey, colKey);
  }
}
Vitaliy Fedorchenko
  • 8,447
  • 3
  • 37
  • 34
  • Thanks Vitaliy. This gets close but the incoming data is a RunningValuePivotTable. The concept works but the running values get mixed up. As soon as you reverse the array the running totals get reversed as well. Is there a way to prevent it from recalculating the running values? It's important that we have their impact from the beginning of the data set but only show the last 3 date periods. – Jason Sep 20 '19 at 18:56
  • @Jason yeah in case when TopPivotTable is combined RunningValuePivotTable this hack will not work. I guess TopPivotTable may be enhanced to support 'last items' mode - you need to contact NReco support on this. – Vitaliy Fedorchenko Sep 23 '19 at 09:10