Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Last non blank in Power Query

Hi,

 

I'm struggling with one of the Query tasks - I need to find Parent Account No according to the Indentation number.

 

I have a table:

No_TotalingIndentationIndentation -1
11..1Z0-1
1111..11Z10
111111..111Z21
1110 32
1118 32
1119 32
112112..112Z21
1120 32
1128 32
1129 32
1616..16Z10
160160..160Z21
16001600..1600Z32
16000 43
16009 43

The algorithm that I what to use is - to find and bring last account No_ where Indentitanion number is [Indentitation -1], if [Indentitation-1] = -1 than Blank.

The result table should look like this:

No_TotalingIndentationIndentation -1Parent No_
11..1Z0-1 
1111..11Z101
111111..111Z2111
1110 32111
1118 32111
1119 32111
112112..112Z2111
1120 32112
1128 32112
1129 32112
1616..16Z101
160160..160Z2116
16001600..1600Z32160
16000 43160
16009 43160

 

I prefer to do this calculation in the Query Editor than DAX, but both solutions are welcome.

  • Anonymous 

     

    Try this method

     

    First add an Index Column

    Then this custom column

     

    if [#"Indentation -1"]>-1 then
    
    Table.Max(
    Table.SelectRows(#"Added Index",(x)=>x[Index]<[Index] and x[Indentation]=[#"Indentation -1"]),
    "Index")[No_]
    else
    null

     

     

2 Replies

  • Zubair_Muhammad's avatar
    Zubair_Muhammad
    Community Champion

    Anonymous 

     

    Try this method

     

    First add an Index Column

    Then this custom column

     

    if [#"Indentation -1"]>-1 then
    
    Table.Max(
    Table.SelectRows(#"Added Index",(x)=>x[Index]<[Index] and x[Indentation]=[#"Indentation -1"]),
    "Index")[No_]
    else
    null