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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
AlexGallet01
Helper IV
Helper IV

It's possible to do a foreach on power bi ?

Hello,

 

I have a problem. I need to do some sort of foreach on power bi desktop because I have a number of commands with different lines and I want to take the last line number of this command but if i go through a function to take the last Line from maxvalue, I just have one result for all the number of commands. So I think I am forced to use some kind of foreach but I do not know if it is possible to do this function on power bi desktop or simular.

1 ACCEPTED SOLUTION
AlexGallet01
Helper IV
Helper IV

Hello, 

 

It's good I've done otherwise. I create a group by command with the maximum value of the row.

View solution in original post

9 REPLIES 9
azarel
Frequent Visitor

Yes, there is a foreach function in PowerBI. Don't know why, but it's labelled as SUMX, as a programmer I don't get this logic, it doesn't sum it evals an expression for each line in the designated table. Anyway, to use it as a foreach parse the target table as the first argument and the operation you want to do as the second. In my case, I'm multiplying the amounts in a value column by a percentile multiplied by the days since the amount was paid to get a simple interest calculation.

As a more concrete example; if you have a booking table where you store room-bookings for a hotel, the date for the booking, the number of guests staying, and the price per person. You can write a measure to calculate the total price by doing SUMX( 'Bookings', 'Bookings'[Guests] * 'Bookings'[Price] ). The cool part about this is that it actually puts a concrete value in per line item that you can tabulate and accurately summarise if you wanted to do a matrix table so you could have daily totals and the like. Before knowing about SUMX I was doing stupid things with MAX which would get accurate line item calculations but could never be summarised, fine for some things but not for others.

AlexGallet01
Helper IV
Helper IV

Hello, 

 

It's good I've done otherwise. I create a group by command with the maximum value of the row.

AlexGallet01
Helper IV
Helper IV

Thank's but i can this result in a column in my table no in schéma

You may have to use a measure and not a column. And you might want to check out MAXX.

https://msdn.microsoft.com/en-us/library/ee634576.aspx

 

All of the "X" functions (SUMX, COUNTX, etc.) are kind of foreach like in that they evaluate something for each row of a table. With the right filters applied, you should be able to achieve the desired result.

 

Also, you may not be thinking enough Power BI here. If you simply created a table visualization and put what you want to summarize by and then the MAX of your other value, you would have what you are looking for.

 

If you truly want a table in the model, then use SUMMARIZE along with MAX.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

It would be easier to come up with something that might help you if you post a sample of the data you're working with, and what you're trying to get out of it.

 

The concept of "foreach", such as in a language like C#, is procedural in nature, whereas both M and DAX are designed to work on sets in a functional manner (i.e. you describe the result, and not the steps it takes to get to the result).

I have a datatable like this

Capture.PNG

 

And for one THKTNODOS i have differents value of THKNNOLIGN. And i want the max value of THKNNOLIGN by THKTNODOS.

Anonymous
Not applicable

There is already a built-in Maximum aggregate for any visualization type that will do exactly what you're looking for. Just make sure that your "THKNNOLIGN" column is a numeric type.

 

PBIDesktop_2017-07-28_08-16-21.png

Greg_Deckler
Super User
Super User

M code or DAX?



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

DAX

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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