Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
qsong
Helper II
Helper II

LOOP to Look up the value in M Query

Hi,

 

I have a following data table:

qsong_1-1597340326257.png

 

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:

 

qsong_0-1597340280964.png

 

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.

 

1 ACCEPTED SOLUTION
v-alq-msft
Community Support
Community Support

Hi, @qsong 

 

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

Table:

d1.png

 

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:

d3.png

 

Best Regards

Allan

 

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

View solution in original post

4 REPLIES 4
v-alq-msft
Community Support
Community Support

Hi, @qsong 

 

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

Table:

d1.png

 

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:

d3.png

 

Best Regards

Allan

 

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

Hi @v-alq-msft ,

 

You answer is very helpful. Thanks a lot.

 

Best, Qianru

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 

 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

ToddChitt
Super User
Super User

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.

 




Did I answer your question? If so, mark my post as a solution. Also consider helping someone else in the forums!

Proud to be a Super User!





Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors