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.
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.
Solved! Go to Solution.
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.
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.
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
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
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.
Proud to be a Super User! | |
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.