Forum Discussion
Custom column Index by two columns group
- 8 years ago
Hi tiagomarciano,
Try this calculated column formula
=CALCULATE(DISTINCTCOUNT(Data[Data]),FILTER(Data,Data[Category]=EARLIER(Data[Category])&&Data[Data]<=EARLIER(Data[Data])))
Hi tiagomarciano,
Try this calculated column formula
=CALCULATE(DISTINCTCOUNT(Data[Data]),FILTER(Data,Data[Category]=EARLIER(Data[Category])&&Data[Data]<=EARLIER(Data[Data])))
For a Power Query solution, you need a double "Group By":
1. On "Categoria" and within these nested tables:
2. On "Data".
The Index can be added to the inner group; once the inner nested tables are expanded, you get the duplicated Index values (same date, same Index value).
After expanding the nested tables per "Categoria", you get the sets of Index values per Categoria.
In the query below, function InnerGroupAddIndex is created for the inner grouping (which also adds the Index and expands the inner nested tables).
Function Value.Type is used twice to make sure that the nested tables have the correct column types.
This is an adjustment of the code that is generated when using "Group By" with operation "All Rows": that code only has "type table", which will reset all column types to "any". The first Value.Type is taken from table "Sorted Rows" with dummy column "Index" added (at this point, this is only required for the column type).
It is a rather complex solution.
let
Source = Table1,
#"Sorted Rows" = Table.Buffer(Table.Sort(Source,{{"Categoria", Order.Ascending}, {"Data", Order.Ascending}})),
#"Grouped Rows" = Table.Group(#"Sorted Rows", {"Categoria"}, {{"AllData", each InnerGroupAddIndex(_), Value.Type(Table.AddColumn(#"Sorted Rows", "Index", each 1, Int64.Type))}}),
InnerGroupAddIndex = (Table as table) as table =>
let
#"Grouped Rows" = Table.Group(Table, {"Data"}, {{"AllData", each _, Value.Type(Table)}}),
#"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "Index", 1, 1),
#"Expanded AllData" = Table.ExpandTableColumn(#"Added Index", "AllData", {"Categoria"}, {"Categoria"})
in
#"Expanded AllData",
#"Expanded AllData" = Table.ExpandTableColumn(#"Grouped Rows", "AllData", {"Data", "Index"}, {"Data", "Index"})
in
#"Expanded AllData"
- aviral8 years ago
Advocate IV
Hi MarcelBeug:
That solution helped me in grouping at three nested levels and rank the rows.
Really thankful for that.
I am now thinking of converting it to a reusabe function.
Would there be any issues with schedule-refresh if we use functions?