How to return the right most value in a row using Power Query

Skip2MiLu

New Member
Joined
Aug 11, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi Guys

I struggling to find an appropriate M function or combination thereof that will return the value of the right most cell in a row that that contains a value.

I am able to do this in excel using the following functions, but not sure how to replicate this in power query.

Any help would be appreciate.

How to get right-most value of row in Excel
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
The best way that I have come up in PQs that I have created is to create a Custom Column that is a series of If statements. So there may be a better route to take,

It would look a little something like this:
Power Query:
=if [Column 3] <> null then [Column 3] else if[Column 2] <> null then [Column 2] else if [Column 1] <> null then [Column 1] else null
 
Upvote 0
If your columns are like the example, you could unpivot those and filter out nulls from the value column. The do a group query to get the maximum year for each id (if the headers are not numeric, you could add an index column, and get the max value of that), merge that query with the original so you only get the latest data, then repivot.

You could also unpivot, fill down, then just filter for the last column header, and merge that with the original data. I imagine you could do something with List.Transform too.
 
Last edited:
Upvote 0
The idea in PQ could be to make a list of each record, remove all nulls and use List.Last on that list.
On my smartphone now, so I'm not able to provide the correct syntax, so I'm hoping the small guidelines are enough.
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    AddIndex = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    AddCustom = Table.AddColumn(AddIndex,"Col", each List.Last(List.RemoveNulls(Record.ToList(Source{[Index]})))),
    OutputTable = Table.FromList(AddCustom[Col], Splitter.SplitByNothing(), {"Last Non-null"}, null, ExtraValues.Error)
in
    OutputTable

Last Non Null.xlsm
ABCDEFGHIJKLM
1Data1Data2Data3Data4Data5Data6Data7Data8Data9Data10Last Non-null
21068086434545
375812014351130130
46999140768888
514112970142150271231112828
6121377426111105833055
7102125138939539115115
83790632955181649128119119
9
10401318888
111344145252
121925133127110110
131396710177174142641124124
141445310111111
15351089145891041036565
1681122122
17
Sheet4
 
Upvote 0
Alternative without index, and the inner let .. in construction could be a nested formula, but I prefer this construction for longer term maintenance.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    GetLast = Table.AddColumn(Source, "LastValue", each
          let
              Record = Source{_}
            , Values= Record.FieldValues(Record)
            , NoNulls = List.RemoveNulls(Values)
            , Last = List.Last(NoNulls)
        in
           Last
        )
in
    GetLast
Output:
Book1
ABCDEFG
1ABCDEFLastValue
2339438884884
3784836116817171
427513121631479387387
5175658651651
6883701683152152
7725526526
8859859
992891332774420552552
10785453453
11286327739691489658658
123688712153838
13975301641641
14454454
1569655655
16783879879
Sheet2
 
Upvote 0
Solution
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    AddIndex = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    AddCustom = Table.AddColumn(AddIndex,"Col", each List.Last(List.RemoveNulls(Record.ToList(Source{[Index]})))),
    OutputTable = Table.FromList(AddCustom[Col], Splitter.SplitByNothing(), {"Last Non-null"}, null, ExtraValues.Error)
in
    OutputTable

Last Non Null.xlsm
ABCDEFGHIJKLM
1Data1Data2Data3Data4Data5Data6Data7Data8Data9Data10Last Non-null
21068086434545
375812014351130130
46999140768888
514112970142150271231112828
6121377426111105833055
7102125138939539115115
83790632955181649128119119
9
10401318888
111344145252
121925133127110110
131396710177174142641124124
141445310111111
15351089145891041036565
1681122122
17
Sheet4

Thanks for all the support. Still a bit new to M language and power query.

So let me use your code to create a custom column and see if works for my use case.

Will test is out this weekend and give feedback, but I looks like all is in order.

Just to give a bit of context on my scenario.

So I want to create 2x customer column using the below data:
  1. Average Price = 2021, 2022 Amt (Column AT) / 2021, 2022 Qty (Column AC)
  2. Latest Price = Right Most Amt (Column AH:AS) / Right Most Qty (Column Q:AB)

    -Also need to zero or null any calculations on rows where there is a 0 or null Amt being divided by Qty with a value (This is in the case of qty samples being shipped without a value amt as in row 7 as an example)

    1628778359941.png
 
Upvote 0
Alternative without index, and the inner let .. in construction could be a nested formula, but I prefer this construction for longer term maintenance.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    GetLast = Table.AddColumn(Source, "LastValue", each
          let
              Record = Source{_}
            , Values= Record.FieldValues(Record)
            , NoNulls = List.RemoveNulls(Values)
            , Last = List.Last(NoNulls)
        in
           Last
        )
in
    GetLast
Output:
Book1
ABCDEFG
1ABCDEFLastValue
2339438884884
3784836116817171
427513121631479387387
5175658651651
6883701683152152
7725526526
8859859
992891332774420552552
10785453453
11286327739691489658658
123688712153838
13975301641641
14454454
1569655655
16783879879
Sheet2

Excellent solution thanks!

1628780602554.png
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top