Forum Discussion

qsong's avatar
qsong
Helper II
6 years ago
Solved

LOOP to Look up the value in M Query

Hi,

 

I have a following data table:

 

I need to create a column called FINAL VALUE with certain rules in the power query editor:

- The FINAL VALUE is created based on the COUNTRY
- If VALUE is greater than 100, then the previous value will be taken
- if the previous values (no previous value such as 202001) are all greater than 100, then 100

 

So the final table should look like:

 

 

I sort COUNTRY and DATE, and then I try to apply loop in M Query. As I am not familiar with the function in M query, I have hard time figuring out. I appreciate any help. Thank you.

 

  • Hi, qsong 

     

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

    Table:

     

    You may go to 'Query Editor'=>'Add Column'=>'Custom Column', input the following codes.

    let
    country=[Country],date=[Date],
    tab = Table.SelectRows(
        #"Changed Type",
        each [Country]=country and [Date]<date and [Value]<100
    )
    in 
    if Table.RowCount(tab)>0 
    then 
       if [Value]>100 
       then Table.Max(tab,"Date")[Value]
       else [Value]
    else 
    if [Value]<100
          then [Value]
          else 100

     

    Result:

     

    Best Regards

    Allan

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

4 Replies

  • v-alq-msft's avatar
    v-alq-msft
    Community Support

    Hi, qsong 

     

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

    Table:

     

    You may go to 'Query Editor'=>'Add Column'=>'Custom Column', input the following codes.

    let
    country=[Country],date=[Date],
    tab = Table.SelectRows(
        #"Changed Type",
        each [Country]=country and [Date]<date and [Value]<100
    )
    in 
    if Table.RowCount(tab)>0 
    then 
       if [Value]>100 
       then Table.Max(tab,"Date")[Value]
       else [Value]
    else 
    if [Value]<100
          then [Value]
          else 100

     

    Result:

     

    Best Regards

    Allan

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • Fowmy's avatar
      Fowmy
      Super User

      Hi v-alq-msft 

      That is a neat solution!

      I am very much interested in improving my M skills; can you direct me to some M language resources for deep learning of the language?

      Thanks
      Fowmy 

       

  • If this was SQL I would say that you can JOIN the table to itself on something OTHER than an EQUAL JOIN.

     

    SELECT * FROM MyTable AS A

    INNER JOIN MyTable AS B

       ON A.Country = B.Country

      AND A.Date < B.Date

     

    That would effectively get you every iteration of a Country/Date plus all previuos Dates.

    Then a GROUP BY the A columns and take the MIN of B.Value

     

    But we're not SQL, sorry.