Hi All,
As you see below, Mynda provided this solution in 2012 (Way to go Mynda). Just wondering if in 2021 that this could be solved in Power Query.
January 12, 2012, by Mynda Treacy
Reference: https://www.myonlinetraininghub.com/index-match-with-a-twist
The Challenge
Christy wanted to find the last value in a row for a specific part number using a table of data in Sheet1 like this:
Notice how some rows don’t have a value in every cell? This is what makes this challenge unique.
You see Christy wanted to find the value for say, part A10106, for the month of June, but if June didn’t have a value then find the value for the previous month, and if May didn’t have a value then go to the previous month and so on.
And it wasn’t just one part, it was over 2000 parts and for every month of the year.
Not being one to shy away from a challenge I persisted, and this is my formula.
The Solution
=IFERROR(INDEX(INDIRECT("Sheet1!"&ADDRESS(MATCH(A4, Sheet1!$A:$A,0),1,1)&":"&ADDRESS(MATCH(A4, Sheet1!$A:$A,0),MATCH(B3, Sheet1!$1:$1,0),1)),MATCH(9.99999999999999E+307,INDIRECT("Sheet1!" &ADDRESS(MATCH(A4, Sheet1!$A:$A,0),1,1)&":"&ADDRESS(MATCH(A4, Sheet1!$A:$A,0),MATCH(B3, Sheet1!$1:$1,0),1)))),0)
Hi Cedric,
Power Query can certainly give you a list of the last value for each part number, but it's not clear from your question as to whether you want to be able to easily switch which month's value you want to return. e.g. in my example I needed to find the value for June and if June didn't have a value, then the previous value. What did you have in mind?
Mynda
Should be easier in PQ.
There are also other ways to get that result with formulas: =LOOKUP(2, 1/OFFSET(Sheet1!B1,MATCH(A4,part_number,0),0,1,MATCH(B3,Sheet1!B1:M1,0)), OFFSET(Sheet1!B1,MATCH(A4,part_number,0),0,1,MATCH(B3,Sheet1!B1:M1,0)))
In PQ:
(x)=> try List.Last(List.RemoveNulls(List.FirstN(Record.ToList(x) ,Pos+1))) otherwise null
File attached.
Hi,
I was just looking for a Power Query solution that accomplished what your formula accomplished. But since you asked, could you switch which month value you wanted to be returned?
I have not had a chance to look at Catalin's solution yet. Busy with some repairs around the homestead.
Thank you for your replies.
Hi Cedric,
Check out Catalin's workbook as it allows you to choose both the part number and month.
Mynda
Hi,
I looked at Catalin's solution, and I'm impressed. Both the formula and Power Query work for any part number and any month. I noted the formula updates automatically, but the Power Query solution must be refreshed if you change part number or month.
I asked this question as a learning experience. However, it is going to take me weeks to figure out that elegant formula. But, it will give e something to do.
A couple questions on the Power Query solution.
= Table.AddColumn(#"Filtered Rows", "Result", (x)=> try List.Last(List.RemoveNulls(List.FirstN(Record.ToList(x) ,Pos+1))) otherwise null)
The “x” in (Record.ToList(x). Does it refer to the “Filtered Rows” table?
The “Pos+1.” I understand the “+1” part, because of the first column, but “Pos,” is that a keyword? Also, “try” and “otherwise,” where do I find information on them?
I am sorry if these are stupid questions, but I’m old and I am just trying to keep my mind active.
Anyway,
Thanks In Advance, Cedric
Hi Cedric,
The x from (x)=> try List.Last(List.RemoveNulls(List.FirstN(Record.ToList(x) ,Pos+1))) otherwise null
represents the current row of the "#Filtered Rows" table (the formula will be applied to each row of the table)
Few things to keep in mind:
-A row from a table is a record
-A column from a table is a list
Because I am referring to the current row (x), I have to apply Record functions (first, I convert the record to list, then I apply list functions: FirstN, RemoveNulls, Last)
If look at the PQ steps, you will see:
Part
Month
Pos (short from Position), it simply calculates the position of the month "Jun" for example from the list of months (table headers), in order to remove subsequent months.
try-otherwise is just the PQ way of saying: IFERROR(formula, "")
You have to explore the PQ documentation, you will find there many examples for each function:
https://docs.microsoft.com/en-us/powerquery-m/power-query-m-function-reference
Hi,
Thank you for taking the time to answer my questions. I should have picked up on the "Pos."
I am working on your formula and found an explanation of "LOOKUP" and finding the last position. Quite complicated. I am using the "Evaluate Formula" feature to try to figure how the "TRUE" - "FALSE" results line up. Something else to keep my busy. I am really impressed with your knowledge.
Have a great day,
Cedric