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

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.

Reply
Villeminl
Regular Visitor

SUMIFS functionality using Power Query M advanced editor

Hello all, in need of some help to recreate the functionality of this SUMIFS formula. I'm still new on M Code and I'm really struggling with this one. I'm using this SUMIFS to compute a running total based on each days transactions.

This is the SUMIFS in question:

"=SUMIFS([QTY],[PART_ID],[@[PART_ID]],[COUNT_REL_OPERATION],[@[COUNT_REL_OPERATION]],[COMPLETED_QTY],">"&[@[COMPLETED_QTY]]) "

 

We could simplify but just saying:

 

"=SUMIFS(SUM RANGE, CRITERIA RANGE 1, CRITERIA 1, CRITERIA RANGE 2, CRITERIA 2, CRITERIA RANGE 3,">"&CRITERIA 3) "

I found stuff online using the Let function but I'm not able to figure out how to include multiple ranges and multiple criterias, especially when one of the critarias has to be > (more than) instead of equal.

Here is where I got so far:

 

let
Range1 = ([PART_ID])
in List.Sum(
Table.SelectRows(#"Changed Type", each [PART_ID] = Range1)
[QTY])

1 ACCEPTED SOLUTION

Hi @Villeminl 

 

My guess is your column [LAST TOUCHED] is added after step #"Changed Type", you are now calling it in this table which the column has not yet existed...so let's call the step to add [LAST TOUCHED] as "step1" for now, then it should be like this

= [CurID = [PART_ID],
CurOp=[COUNT_REL_OPERATION],
CurQty=[COMPLETED_QTY],
CurDate=[DUE DATE],
CurTouch=[LAST TOUCHED],
res=List.Sum(
Table.SelectRows(step1, each [PART_ID]=CurID and [COUNT_REL_OPERATION] =CurOp and [COMPLETED_QTY] = CurQty and DateTime.Date([DUE DATE]) = CurDate and [LAST TOUCHED] < CurTouch)
[QTY])][res]

 

As @AlexisOlson mentioned, if it is slow, especially if you have lots of data, you should look for alternative ways. BUT, still, you can take it as a chance to learn, a step in M can be a table, a list, a parameter...etc

 

I've sent you my email, feel free if you need more help.

View solution in original post

8 REPLIES 8
Vera_33
Resident Rockstar
Resident Rockstar

Hi @Villeminl 

 

if I understand correctly, you want to do something like this

= Table.AddColumn(#"Changed Type", "Custom", each [CurID = [PART_ID],
CurOp=[COUNT_REL_OPERATION],
CurQty=[COMPLETED_QTY],
res=List.Sum(
Table.SelectRows(#"Changed Type", each [PART_ID]=CurID and [COUNT_REL_OPERATION] =CurOp and [COMPLETED_QTY] > CurQty)
[QTY])][res])

Here is what I'm trying and where I'm getting an error:

= Table.AddColumn(#"Changed Type", "SUMIF3", each [CurID = [PART_ID],

CurOp=[COUNT_REL_OPERATION],

CurQty=[COMPLETED_QTY],

CurDate=[DUE DATE],

CurTouched=[LAST TOUCHED],

res=List.Sum(Table.SelectRows(#"Changed Type", each [PART_ID]=CurID and [COUNT_REL_OPERATION] =CurOp and [COMPLETED_QTY] =CurQty and [DUE DATE] = CurDate and [LAST TOUCHED] < CurTouched)

[QTY])][res]))

 

"Expression.Error: The field 'LAST TOUCHED' of the record wasn't found."

 

[LAST TOUCHED] is a custom column that was previously added in my main Query.

This custom column is calculated using fields from 3 queries that I merged in my main Query,

Not sure if this has an incidence...

 

Hi @Villeminl 

 

What do you mean by "This custom column is calculated using fields from 3 queries that I merged in my main Query"? You are now in the query - step #"Changed Type", is [LAST TOUCHED] not a column in this query?

Yes, [LAST TOUCHED] is column in this query. It's custom column added during the previous steps

For some reasons I'm getting this error message where the field 'LAST TOUCHED' of the record is not being found,

 

I'm doing this:

= [CurID = [PART_ID],
CurOp=[COUNT_REL_OPERATION],
CurQty=[COMPLETED_QTY],
CurDate=[DUE DATE],
CurTouch=[LAST TOUCHED],
res=List.Sum(
Table.SelectRows(#"Changed Type", each [PART_ID]=CurID and [COUNT_REL_OPERATION] =CurOp and [COMPLETED_QTY] = CurQty and DateTime.Date([DUE DATE]) = CurDate and [LAST TOUCHED] < CurTouch)
[QTY])][res]

 

and the result:

 

Expression.Error: The field 'LAST TOUCHED' of the record wasn't found

 

Hi @Villeminl 

 

My guess is your column [LAST TOUCHED] is added after step #"Changed Type", you are now calling it in this table which the column has not yet existed...so let's call the step to add [LAST TOUCHED] as "step1" for now, then it should be like this

= [CurID = [PART_ID],
CurOp=[COUNT_REL_OPERATION],
CurQty=[COMPLETED_QTY],
CurDate=[DUE DATE],
CurTouch=[LAST TOUCHED],
res=List.Sum(
Table.SelectRows(step1, each [PART_ID]=CurID and [COUNT_REL_OPERATION] =CurOp and [COMPLETED_QTY] = CurQty and DateTime.Date([DUE DATE]) = CurDate and [LAST TOUCHED] < CurTouch)
[QTY])][res]

 

As @AlexisOlson mentioned, if it is slow, especially if you have lots of data, you should look for alternative ways. BUT, still, you can take it as a chance to learn, a step in M can be a table, a list, a parameter...etc

 

I've sent you my email, feel free if you need more help.

Hi @Villeminl 

 

My bad, you pasted all the code (I did from the formula bar) to the column, it should be like this, it is a column, not a table...

[CurID = [PART_ID],
CurOp=[COUNT_REL_OPERATION],
CurQty=[COMPLETED_QTY],
res=List.Sum(
Table.SelectRows(#"Changed Type", each [PART_ID]=CurID and [COUNT_REL_OPERATION] =CurOp and [COMPLETED_QTY] > CurQty)
[QTY])][res]

Vera_33_0-1643984400787.png

 

This looks like a good translation of SUMIFS as was requested and should work nicely on small datasets.

 

@Villeminl If this approach is too slow on your particular dataset, you may wish to ask a separate question about how to achieve your end goal efficiently rather than how to replicate a specific function.

Thanks for your input.

I tried it and it worked. It actually created a new comlumn as an imbedded table that I can expend.

Once I expend it duplicates all the rows from my original table so I decided to move that new customer column to a new query (right click; Add as new query) so I can then combine both queries and avoid those duplicates.

Although I noticed that when expeding the table, some fields (custom added columns) were missing, I tried to tweak the code you gave me to add some extra calculated fields and it resulted in an error as the fields couldn't be found.

Would you know why some custom columns added in the original query are disapearing from the results of the table coming from your code?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors