Forum Discussion
Dynamic Index
- 3 years ago
The code below will start the Index column at the first Non-Null in the FM+ column.
Read the code comments as the code can be simplified if nulls will ONLY be at the beginning of the column and not interspersed within the column.
Since it does not depend on a Join (merge), there is no issue with duplicates.
let Source = Table.FromColumns( {{1..9}, {null,null, 2,5,9,14,null,27,35}}, type table[FM=Int64.Type, #"FM+"=Int64.Type]), //Add index starting at first non-blank entry in FM+ //use next line if there will never be nulls except at the beginning of the column // #"FM+ 1st" = Table.RowCount(Source) - List.NonNullCount(Source[#"FM+"]), //if nulls could be anywhere but we need to start at the First non-null then #"FM+ 1st" = List.Max( List.Accumulate( List.PositionOf(Source[#"FM+"],null,Occurrence.All), {}, (state, current)=> if state = {} and current=0 then {0} else if List.Last(state) + 1 = current then state & {current} else state & {null} ))+1, #"Index List" = List.Repeat({null}, #"FM+ 1st") & {1..Table.RowCount(Source) - #"FM+ 1st"}, #"Add Index" = Table.FromColumns( Table.ToColumns(Source) & {#"Index List"}, type table[FM=Int64.Type, #"FM+"=Int64.Type, Index=Int64.Type] ) in #"Add Index"Example
The code below will start the Index column at the first Non-Null in the FM+ column.
Read the code comments as the code can be simplified if nulls will ONLY be at the beginning of the column and not interspersed within the column.
Since it does not depend on a Join (merge), there is no issue with duplicates.
let
Source = Table.FromColumns(
{{1..9},
{null,null, 2,5,9,14,null,27,35}},
type table[FM=Int64.Type, #"FM+"=Int64.Type]),
//Add index starting at first non-blank entry in FM+
//use next line if there will never be nulls except at the beginning of the column
// #"FM+ 1st" = Table.RowCount(Source) - List.NonNullCount(Source[#"FM+"]),
//if nulls could be anywhere but we need to start at the First non-null then
#"FM+ 1st" =
List.Max(
List.Accumulate(
List.PositionOf(Source[#"FM+"],null,Occurrence.All),
{}, (state, current)=>
if state = {} and current=0 then {0}
else if List.Last(state) + 1 = current then state & {current}
else state & {null}
))+1,
#"Index List" = List.Repeat({null}, #"FM+ 1st") & {1..Table.RowCount(Source) - #"FM+ 1st"},
#"Add Index" = Table.FromColumns(
Table.ToColumns(Source) & {#"Index List"},
type table[FM=Int64.Type, #"FM+"=Int64.Type, Index=Int64.Type]
)
in
#"Add Index"
Example
- marsclone3 years ago
Helper IV
Thank you for your help.
The problem that i have is , that FM and FM+ are measures. I have these in Power Pivot, so not visible in Power Query.
I didn't thought it was so hard, to add a subsequent number?
I have added the file: Testfile
So FM is a sum of some values. When "Opmerking" has an "x" then FM+ is null, when not then take value and in the next row plus previous value. When FM+ has an value then start a simple index for the following rows.
I hope someone can help with this?
desired outcome: