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
YardOfTheYard
New Member

Calculate sum of values in one column based on values in another column (SUMIF / SUMPROD

Hi all, I am creating an inventory outlook model. I have shaped my data in PQ. For each SKU/partnumber, have a Stock figure for each week in the next 12 weeks, and I have a Sales volume for each week in the next 12 weeks.

 

For each week, I want to calculate:

i. the average Sales for the next 4 weeks

ii. therefore, the coverage as calculated by Stock divided average-4-week-sales

 

My query looks like this:

 
YardOfTheYard_1-1726141742336.png

 

In an Excel Table, I would create:

 

a. a column with a SUMIFS to calculate, for the rows where the RowType3 is "Stock", the Average of the values in the Value column where the SKU is the same, and the tblWks.WC column date is between the current row date and that+28 days.

 

=IF([@RowType3]="Stock",AVERAGEIFS([Value],[RowType3],"Sales",[tblWks.WC],">"&[@[tblWks.WC]],[tblWks.WC],"<="&[@[tblWks.WC]]+28),0)

 

Example result: for the Stock on 02/09, the average sales of the next 4 weeks (the average of 135,416,107,107) would be 191.

 

 

b. a column to divide, for the rows where the RowType3 is "Stock" and the SKU is the same, the Value divided by the previously-created Average sales column

 

=IF([@RowType3]="Stock",[@Value]/[@Avg4WkSales],0)

 

Example result: for the Stock on 02/09, the stock is 868, and 868/191 = 4.5 weeks of cover

 

 

How do I achieve this in PowerQuery please? I'm sure it's a combination of filter/grouping, but I can't get my head around it!

 

Notes:

1. Ideally I want this calculation to be within the DataModel as a measure/DAX, so that the calculation can be made at the level of SKU or, when relationships are created, at other levels of aggregation.

2. The data set is much larger than this, of course.

3. The arbitary "4" weeks is something I would want to flex later, but for now I look simply for the approach!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @YardOfTheYard ,

Is there something wrong with your expected result? You need to calculate the average value for the next 4 weeks, so the Avg corresponding to the last date of each sales segment should not exist. It seems that you should move up one data point from the second date?

vjunyantmsft_1-1726209422132.png


Here is my solution:
Use this M code to create a custom column:

let 
   current_Shippingcode = [Shipping code],
   current_date = [tblWks.WC]
in 
   List.Average(
       Table.SelectRows(
       #"Changed Type",
       each [Shipping code] = current_Shippingcode and [RowType3] = "Sales" and [tblWks.WC] > current_date and [tblWks.WC] <= Date.AddDays(current_date, 28)
   )[Value]
)

And the output is as below:

vjunyantmsft_2-1726209478252.png

Then use this M code to create another custom column:

if [RowType3] = "Sales" then 0 else [Value] / [Avg4WkSales]

And the final output is as below:

vjunyantmsft_3-1726209672025.png


Best Regards,
Dino Tao
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

5 REPLIES 5
SBAJW
Regular Visitor

Thanks @Anonymous for the suggested solution. I figured out how to insert the M-Code, but I wonder if this is an inefficient solution - the query now takes over a minute to refresh, and this is only for a small subset of the data that I used for this forum post. 

YardOfTheYard
New Member

Hi, I paste a stripped down example table here, if this makes it easier to answer? Here there are just two Shipping Codes, one SourceFileName and six weeks of data for Sales and Stock (to keep the example small).

 

The last two columns are the results I imagine I want (as achieved in Excel), but I am open to any solution.

 

SourceFileNameShipping codeRowType3tblWks.WCValueAvg4WkSalesStockCover
DF091564Sales02/09/20240910.0
DF091564Sales09/09/2024203910.0
DF091564Sales16/09/2024162500.0
DF091564Sales23/09/20240700.0
DF091564Sales30/09/20240940.0
DF091564Sales07/10/2024371410.0
DF091564Sales14/10/20242452450.0
DF091564Stock02/09/2024895919.8
DF091564Stock09/09/2024692917.6
DF091564Stock16/09/20245305010.7
DF091564Stock23/09/2024560707.9
DF091564Stock30/09/2024560946.0
DF091564Stock07/10/20245231413.7
DF091564Stock14/10/20242782451.1
DF091576Sales02/09/20245903120.0
DF091576Sales09/09/20241351910.0
DF091576Sales16/09/20244163160.0
DF091576Sales23/09/20241072320.0
DF091576Sales30/09/20241072740.0
DF091576Sales07/10/20246343580.0
DF091576Sales14/10/202483830.0
DF091576Stock02/09/20248683122.8
DF091576Stock09/09/20247331913.8
DF091576Stock16/09/20243213161.0
DF091576Stock23/09/20247442323.2
DF091576Stock30/09/20246852742.5
DF091576Stock07/10/2024813580.2
DF091576Stock14/10/2024461835.6
Anonymous
Not applicable

Hi @YardOfTheYard ,

Is there something wrong with your expected result? You need to calculate the average value for the next 4 weeks, so the Avg corresponding to the last date of each sales segment should not exist. It seems that you should move up one data point from the second date?

vjunyantmsft_1-1726209422132.png


Here is my solution:
Use this M code to create a custom column:

let 
   current_Shippingcode = [Shipping code],
   current_date = [tblWks.WC]
in 
   List.Average(
       Table.SelectRows(
       #"Changed Type",
       each [Shipping code] = current_Shippingcode and [RowType3] = "Sales" and [tblWks.WC] > current_date and [tblWks.WC] <= Date.AddDays(current_date, 28)
   )[Value]
)

And the output is as below:

vjunyantmsft_2-1726209478252.png

Then use this M code to create another custom column:

if [RowType3] = "Sales" then 0 else [Value] / [Avg4WkSales]

And the final output is as below:

vjunyantmsft_3-1726209672025.png


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

Hi, really basic question, but I already have M-code for the query...

 

 

let
    Source = OverseasFUP,
    #"Filtered Rows3" = Table.SelectRows(Source, each [WkNumFUP] >= 202436 and [WkNumFUP] <= 202442),
    #"Filtered Rows1" = Table.SelectRows(#"Filtered Rows3", each ([Shipping code] = "300001564" or [Shipping code] = "300001576")),
    #"Extracted Last Characters" = Table.TransformColumns(#"Filtered Rows1", {{"Shipping code", each Text.End(_, 4), type text}}),
    #"Merged Queries" = Table.NestedJoin(#"Extracted Last Characters", {"RowType1"}, tblRowTypes, {"RowType1"}, "tblRowTypes", JoinKind.LeftOuter),
    #"Expanded tblRowTypes" = Table.ExpandTableColumn(#"Merged Queries", "tblRowTypes", {"RowType3"}, {"RowType3"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded tblRowTypes", each ([RowType3] = "Sales" or [RowType3] = "Stock")),
    #"Merged Queries1" = Table.NestedJoin(#"Filtered Rows", {"WkNumFUP"}, tblWks, {"WkNumFUP"}, "tblWks", JoinKind.LeftOuter),
    #"Expanded tblWks" = Table.ExpandTableColumn(#"Merged Queries1", "tblWks", {"WC"}, {"tblWks.WC"}),
    #"Sorted Rows" = Table.Sort(#"Expanded tblWks",{{"Shipping code", Order.Ascending}, {"RowType3", Order.Ascending}, {"tblWks.WC", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"RowType1", "WkNumFUP"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"SourceFileName", "Shipping code", "RowType3", "tblWks.WC", "Value"})
in
    #"Reordered Columns"

 

I don't understand where to paste the M-code you mentioned?

SBAJW
Regular Visitor

I assume you mean to paste it in as a nested "let...in"......but it seems to want a token comma ","

I am lost!

Thanks

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.