cancel
Showing results for 
Search instead for 
Did you mean: 
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.

View solution in original post

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!

Proud to be a Super User!

Website   YouTube    LinkedIn
ToddChitt
Post Prodigy
Post Prodigy

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.

 

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

Get Ready for Power BI Dev Camp

Power BI Dev Camp - June 24th

Mark your calendars and join us for our next Power BI Dev Camp!

Top Solution Authors
Top Kudoed Authors