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

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Anonymous
Not applicable

Calculate table with loop

hello PBI masters,

 

I am a n00b of PBI and struggling with creation of a calculated table. My starting dataset is a table that, for every item and every month, provides specific information:

item
item description
FY
Month
dimension
Quantity
ABC
ABC-Description
2021
1
Ordered
55
ABC
ABC-Description
2021
1
forecast current month
60
ABC
ABC-Description
2021
1
forecast m-1
70
ABC
ABC-Description
2021
1
forecast m-2
70
ABC
ABC-Description
2021
1
forecast m-3
100

 

The calculated table I need is caculating, for every item and month, the difference between the row "Ordered" with each of the rows "forecast", i.e. the column "FCA" in the below example.

item
year
month
FCA
Iterm ABC
2021
1
Ordered - forecast current month
item ABC
2021
1
Ordered - forecast m-1
item ABC
2021
1
Ordered - forecast m-2
item ABC
2021
1
Ordered - forecast m-3

 

as far as I understand, it should require DAX and a loop / for cycle, but have no experience in this language. Anyone that could help me please?

 

thanks a lot!!!

1 ACCEPTED SOLUTION
PaulDBrown
Community Champion
Community Champion

I suggest you change the structure of the original table in Power Query by pivoting the "Dimension" and "Quantity" columns as follows:

pivot.gif

 I would then suggest you create new dimension tables for Item using:

 

Item Table = 
SUMMARIZE('Source Table', 'Source Table'[item], 'Source Table'[item description])

 

item table.png

 

and for Year and Month following this pattern:

 

Fiscal Year Table = DISTINCT('Source Table'[FY])

 

Set up the model using single direction one-to-many relationships between the dimension tables:

model.pngCreate the measures you need following this pattern:

 

Ordered - forecast month = SUM('Source Table'[Ordered])  - SUM('Source Table'[forecast current month])
Ordered - forecast m-1 = SUM('Source Table'[Ordered])  - SUM('Source Table'[forecast m-1])

 

Finally set up a matrix using the fields from the dimension tables and the measures. In the formatting pane, under Values -> Options, turn on the option "Switch values on rows" to get:

result.png

Attached is the sample PBIX file 

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






View solution in original post

2 REPLIES 2
Anonymous
Not applicable

works perfectly. Thanks

PaulDBrown
Community Champion
Community Champion

I suggest you change the structure of the original table in Power Query by pivoting the "Dimension" and "Quantity" columns as follows:

pivot.gif

 I would then suggest you create new dimension tables for Item using:

 

Item Table = 
SUMMARIZE('Source Table', 'Source Table'[item], 'Source Table'[item description])

 

item table.png

 

and for Year and Month following this pattern:

 

Fiscal Year Table = DISTINCT('Source Table'[FY])

 

Set up the model using single direction one-to-many relationships between the dimension tables:

model.pngCreate the measures you need following this pattern:

 

Ordered - forecast month = SUM('Source Table'[Ordered])  - SUM('Source Table'[forecast current month])
Ordered - forecast m-1 = SUM('Source Table'[Ordered])  - SUM('Source Table'[forecast m-1])

 

Finally set up a matrix using the fields from the dimension tables and the measures. In the formatting pane, under Values -> Options, turn on the option "Switch values on rows" to get:

result.png

Attached is the sample PBIX file 

 





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.