Is there a SUMIFS equivalent in Power Query?

JVL

New Member
Joined
Nov 20, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Is there an equivalent for SUMIFS formula in Power Query? I would like to calculate the yellow column in Power Query which includes a cumulative sum for each account for each month and year. Grouping the columns does not work as I want to have both the monthly and the cumulative value in the data. How do I do this?
 

Attachments

  • Sample PQ.JPG
    Sample PQ.JPG
    63.1 KB · Views: 548

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tbl = Table.AddColumn(Source, "YTD", (x)=> List.Sum(Table.SelectRows(Source, each _[Account] = x[Account] and _[Month]<=x[Month])[Value]))
in
    tbl

Book4
ABCDEFGHIJK
1AccountYearMonthValueAccountYearMonthValueYTD
2A20201100A20201100100
3A20202100A20202100200
4A20203100A20203100300
5A20204100A20204100400
6A20205100A20205100500
7A20206250A20206250750
8A20207100A20207100850
9A20208100A20208100950
10A20209100A202091001050
11A202010100A2020101001150
12A202011100A2020111001250
13A202012100A2020121001350
14B20201100B20201100100
15B20202100B20202100200
16B20203100B20203100300
17B2020490B2020490390
18B20205100B20205100490
19B20206100B20206100590
20B20207100B20207100690
21B20208100B20208100790
22B20209100B20209100890
23B202010100B202010100990
24
Sheet2
 
Upvote 0
Solution
This works perfectly! Thank you! I even managed to make YTD for different years.

Could you be able to provide a brief run through of the formula? And do you have suggestions how to calculate last twelve months if I have data for several years?
 
Upvote 0
YW. The Table.SelectRows returns a filtered table, in this case filtered to Account= Account name in row x AND Month <= the Month in row x.
(so as it steps through the rows the filtered table will have just one row for row 0, two for row 1 ... twelve for row11, one for row 12, two for row 13, ...)

List.Sum adds up the Value column in that filtered table.

Table.AddColumn adds the YTD column consisting of each of those List.Sum values.

If you send an xl2bb (or something else that I can paste into excel, i.e., not a picture) of a sample initial table and what the transformed table should look like, I'll revise the query based on the examples.
 
Upvote 0
Your help is much appreciated!

Attached is a sample of a situation I'm usually dealing. The first table is the source data which can come from external excels or source sheets in the same workbook. First I usually need to make some modifications to the data in Power Query such as separating company and account and time to years and months. Sometimes these modifications need to be more complex because I usually receive data in different formats. Only after the modifications I would need to add YTD and LTM columns to the query by company, account, year and month.

Thank you in advance!

Sample.xlsx
ABCDEFGHIJKL
1ItemTime Value CompanyAccount nameAccount numberYearMonthValueYTD by company, account, year and monthLTM by company, account, year and month2
2Company A - Account 1002020-0150Company A Account1002020150
3Company A - Account 1002020-0220Company A Account1002020220
4Company A - Account 1002020-0320Company A Account1002020320
5Company A - Account 1002020-0420Company A Account1002020420
6Company A - Account 1002020-0520Company A Account1002020520
7Company A - Account 1002020-0620Company A Account1002020620
8Company A - Account 1002020-0720Company A Account1002020720
9Company A - Account 1002020-08100Company A Account10020208100
10Company A - Account 1002020-0920Company A Account1002020920
11Company A - Account 1002020-1020Company A Account10020201020
12Company A - Account 1002020-1120Company A Account10020201120
13Company A - Account 1002020-1220Company A Account10020201220
14Company A - Account 2002020-0140Company A Account2002020140
15Company A - Account 2002020-0240Company A Account2002020240
16Company A - Account 2002020-0340Company A Account2002020340
17Company A - Account 2002020-0440Company A Account2002020440
18Company A - Account 2002020-0540Company A Account2002020540
19Company A - Account 2002020-0640Company A Account2002020640
20Company A - Account 2002020-07(200)Company A Account20020207(200)
21Company A - Account 2002020-0840Company A Account2002020840
22Company A - Account 2002020-0940Company A Account2002020940
23Company A - Account 2002020-10300Company A Account200202010300
24Company A - Account 2002020-1140Company A Account20020201140
25Company A - Account 2002020-1240Company A Account20020201240
26Company A - Account 1002019-0110Company A Account1002019110
27Company A - Account 1002019-0210Company A Account1002019210
28Company A - Account 1002019-0310Company A Account1002019310
29Company A - Account 1002019-0410Company A Account1002019410
30Company A - Account 1002019-0510Company A Account1002019510
31Company A - Account 1002019-0610Company A Account1002019610
32Company A - Account 1002019-0710Company A Account1002019710
33Company A - Account 1002019-0850Company A Account1002019850
34Company A - Account 1002019-0910Company A Account1002019910
35Company A - Account 1002019-1010Company A Account10020191010
36Company A - Account 1002019-1110Company A Account10020191110
37Company A - Account 1002019-1210Company A Account10020191210
38Company A - Account 2002019-0120Company A Account2002019120
39Company A - Account 2002019-0220Company A Account2002019220
40Company A - Account 2002019-0320Company A Account2002019320
41Company A - Account 2002019-0420Company A Account2002019420
42Company A - Account 2002019-0520Company A Account2002019520
43Company A - Account 2002019-0620Company A Account2002019620
44Company A - Account 2002019-07(100)Company A Account20020197(100)
45Company A - Account 2002019-0820Company A Account2002019820
46Company A - Account 2002019-0920Company A Account2002019920
47Company A - Account 2002019-10150Company A Account200201910150
48Company A - Account 2002019-1120Company A Account20020191120
49Company A - Account 2002019-1220Company A Account20020191220
50Company B - Account 1002020-0180Company B Account1002020180
51Company B - Account 1002020-0280Company B Account1002020280
52Company B - Account 1002020-0380Company B Account1002020380
53Company B - Account 1002020-0480Company B Account1002020480
54Company B - Account 1002020-0580Company B Account1002020580
55Company B - Account 1002020-0680Company B Account1002020680
56Company B - Account 1002020-0780Company B Account1002020780
57Company B - Account 1002020-08400Company B Account10020208400
58Company B - Account 1002020-0980Company B Account1002020980
59Company B - Account 1002020-1080Company B Account10020201080
60Company B - Account 1002020-1180Company B Account10020201180
61Company B - Account 1002020-1280Company B Account10020201280
62Company B - Account 2002020-01160Company B Account20020201160
63Company B - Account 2002020-02160Company B Account20020202160
64Company B - Account 2002020-03160Company B Account20020203160
65Company B - Account 2002020-04160Company B Account20020204160
66Company B - Account 2002020-05160Company B Account20020205160
67Company B - Account 2002020-06160Company B Account20020206160
68Company B - Account 2002020-07(800)Company B Account20020207(800)
69Company B - Account 2002020-08160Company B Account20020208160
70Company B - Account 2002020-09160Company B Account20020209160
71Company B - Account 2002020-101,200Company B Account2002020101,200
72Company B - Account 2002020-11160Company B Account200202011160
73Company B - Account 2002020-12160Company B Account200202012160
74Company B - Account 1002020-0160Company B Account1002020160
75Company B - Account 1002020-0260Company B Account1002020260
76Company B - Account 1002020-0360Company B Account1002020360
77Company B - Account 1002020-0460Company B Account1002020460
78Company B - Account 1002020-0560Company B Account1002020560
79Company B - Account 1002020-0660Company B Account1002020660
80Company B - Account 1002020-0760Company B Account1002020760
81Company B - Account 1002020-08120Company B Account10020208120
82Company B - Account 1002020-0960Company B Account1002020960
83Company B - Account 1002020-1060Company B Account10020201060
84Company B - Account 1002020-1160Company B Account10020201160
85Company B - Account 1002020-1260Company B Account10020201260
86Company B - Account 2002019-0140Company B Account2002019140
87Company B - Account 2002019-0240Company B Account2002019240
88Company B - Account 2002019-0340Company B Account2002019340
89Company B - Account 2002019-0440Company B Account2002019440
90Company B - Account 2002019-0540Company B Account2002019540
91Company B - Account 2002019-0640Company B Account2002019640
92Company B - Account 2002019-0740Company B Account2002019740
93Company B - Account 2002019-08200Company B Account20020198200
94Company B - Account 2002019-0940Company B Account2002019940
95Company B - Account 2002019-1040Company B Account20020191040
96Company B - Account 2002019-1140Company B Account20020191140
97Company B - Account 2002019-1240Company B Account20020191240
98Company B - Account 1002019-0180Company B Account1002019180
99Company B - Account 1002019-0280Company B Account1002019280
100Company B - Account 1002019-0380Company B Account1002019380
101Company B - Account 1002019-0480Company B Account1002019480
102Company B - Account 1002019-0580Company B Account1002019580
103Company B - Account 1002019-0680Company B Account1002019680
104Company B - Account 1002019-07(400)Company B Account10020197(400)
105Company B - Account 1002019-0880Company B Account1002019880
106Company B - Account 1002019-0980Company B Account1002019980
107Company B - Account 1002019-10600Company B Account100201910600
108Company B - Account 1002019-1180Company B Account10020191180
109Company B - Account 1002019-1280Company B Account10020191280
110Company B - Account 1002019-0130Company B Account1002019130
111Company B - Account 1002019-0230Company B Account1002019230
112Company B - Account 1002019-0330Company B Account1002019330
113Company B - Account 1002019-0430Company B Account1002019430
114Company B - Account 1002019-0530Company B Account1002019530
115Company B - Account 1002019-0630Company B Account1002019630
116Company B - Account 1002019-0730Company B Account1002019730
117Company B - Account 1002019-0860Company B Account1002019860
118Company B - Account 1002019-0930Company B Account1002019930
119Company B - Account 1002019-1030Company B Account10020191030
120Company B - Account 1002019-1130Company B Account10020191130
121Company B - Account 1002019-1230Company B Account10020191230
DATA
 
Upvote 0
try (make sure Value column name is "Value" and not " Value ")

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Split1 = Table.SplitColumn(Source, "Item", Splitter.SplitTextByDelimiter(" - ", QuoteStyle.Csv), {"Company", "AccountName"}),
    Split2 = Table.SplitColumn(Split1, "AccountName", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"AccountName", "AccountNumber"}),
    Split3 = Table.SplitColumn(Split2, "Time", Splitter.SplitTextByDelimiter("-", QuoteStyle.Csv), {"Year", "Month"}),
    tblType = Table.TransformColumnTypes(Split3,{{"Year", Int64.Type}, {"Month", Int64.Type}, {"Value", type number}}),
    tbl = Table.AddColumn(tblType, "YTD", (x)=> List.Sum(Table.SelectRows(tblType, each _[Company] = x[Company] and _[AccountNumber] = x[AccountNumber] and 
        _[Year]=x[Year] and  _[Month]<=x[Month])[Value])),
    tbl1 = Table.AddColumn(tbl, "LTM", (x)=> List.Sum(Table.SelectRows(tbl, each _[Company] = x[Company] and _[AccountNumber] = x[AccountNumber] and 
        #date(_[Year],_[Month],1) <=#date(x[Year],x[Month],1) and #date(_[Year]+1,_[Month],1)> #date(x[Year],x[Month],1))[Value]))
in
    tbl1
 
Upvote 0
This is excellent and works perfectly! Thank you!
 
Upvote 0
~2 years later this solution still works.

I have a hard time understanding what the (x)=> and x in the formula do, but it works for me ¯\_(ツ)_/¯
 
Upvote 0
~2 years later this solution still works.

I have a hard time understanding what the (x)=> and x in the formula do, but it works for me ¯\_(ツ)_/¯
The Table.AddColumn() function has two parameters. The first one is a table value to which the new column will be added, and the second one is a function that generates the new column values.

In M language, you can define a function like this, where 'a' and 'b' are the function parameters:
Power Query:
(a, b) => a + b
The proper version of the same function with value types specified is:
Power Query:
(a as number, b as number) as number => a + b

Like many other native functions in the M language, function closures are used to iterate through the given source's elements. It takes each element of the provided source value (a table and its rows in this case) and uses each item to create the new structure (a column here). The iterator function takes this element as its only parameter. In this case, 'x' represents the table rows for the Table.AddColumn() function. Each 'x' in the function refers to the row in the current iteration.

Take a look at the following simple query, which takes a list and adds 1 to each item of the list to create an updated list with the Result identifier:
Power Query:
let
    Source = {1,2,3},
    Result = List.Transform(Source, (x) => x + 1)
in
    Result

You would often see an "each" expression and an underscore instead. The following query is exactly the same as the previous one, except the "each" expression is used as a shorthand and underscore instead of 'x' below. This is because the generator function in the host function (List.Transform()) is an undeclared function closure that requires a single parameter (list items in this case) defined by the host function in its own scope. This single parameter is represented by the underscore when the "each" expression is used:
Power Query:
let
    Source = {1,2,3},
    Result = List.Transform(Source, each _ + 1)
in
    Result

In the code that answered this question (by @JGordon11), the (x) => notation is used because there is an inner function, Table.SelectRows(), that will recognize the underscore as its own parameter in its own scope. To access the current row's Account record field in the second function, x must be explicitly passed as the function parameter.

Power Query:
tbl = Table.AddColumn(Source, "YTD", (x)=> List.Sum(Table.SelectRows(Source, each _[Account] = x[Account] and _[Month]<=x[Month])[Value]))

I hope this explanation hasn't made it more complicated, but this is the short story of generator/iterator functions and hopefully, it helps.
 
Upvote 0
It's going to take me some time to read through that enough times to get it fully, but manalive that was more than I expected! Thank you!
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,665
Members
449,462
Latest member
Chislobog

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top