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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Find sum by grouping on different columns

Hello Community,

 

I have a Power BI report with different tabs. I have built a tab where the data is coming from 3 different tables, 2 of them from sql server (fact tables) and 1 from a custom made table using power query. Columns 'WeekNum' and 'Year' comes from the custom made table and is of text data type and whole number data type. Below is the sample table provided:

 

DateWeekNumYearLocationNoLocationNameCityProvinceMarketRegionDivisionAmount
11/29/2019120191abcWaukeganILChicagoCentralRUTE$123.00
11/29/201912019412defParsonsKSJoplin-PittsburgCentralRUTP$55.00
11/30/2019120193902ghiMiltonWIMilwaukeeCentralRUTE$54.60
11/30/2019120193961jklGunnisonUTSalt Lake CityWestNRUTTech$89.00
11/30/2019120194016mnoCaryILChicagoCentralRUTE$45.00
12/1/2019120191abcWaukeganILChicagoCentralRUTE$14.00
12/1/2019120193902ghiMiltonWIMilwaukeeCentralRUTE$36.80
12/2/201912019349vwxColumbianaOHAkronNortheastRUTE$75.30
12/2/2019120193961jklGunnisonUTSalt Lake CityWestNRUTTech$981.00
12/2/201912019487fghClaytonGAAtlantaSoutheastRUTE$33.00
12/3/2019120191abcWaukeganILChicagoCentralRUTE$99.55
12/3/201912019412defParsonsKSJoplin-PittsburgCentralRUTP$0.98
12/4/2019120194016mnoCaryILChicagoCentralRUTE$90.02
12/4/2019120193961jklGunnisonUTSalt Lake CityWestNRUTTech$34.50
12/4/201912019487fghClaytonGAAtlantaSoutheastRUTE$2.90
12/4/2019120193902ghiMiltonWIMilwaukeeCentralRUTE$6.57
12/5/2019120191abcWaukeganILChicagoCentralRUTE$15.56
12/5/201912019412defParsonsKSJoplin-PittsburgCentralRUTP$58.87
12/6/2019220193961jklGunnisonUTSalt Lake CityWestNRUTTech$21.60
12/6/201922019412defParsonsKSJoplin-PittsburgCentralRUTP$13.60
12/7/201922019487fghClaytonGAAtlantaSoutheastRUTE$11.35
12/7/2019220191abcWaukeganILChicagoCentralRUTE$312.00
12/7/2019220193961jklGunnisonUTSalt Lake CityWestNRUTTech$10.46
12/8/201922019487fghClaytonGAAtlantaSoutheastRUTE$75.60
12/8/2019220193961jklGunnisonUTSalt Lake CityWestNRUTTech$16.56
11/27/2020120204016mnoCaryILChicagoCentralRUTE$6.98
11/27/2020120201abcWaukeganILChicagoCentralRUTE$300.00
11/28/202012020412defParsonsKSJoplin-PittsburgCentralRUTP$20.65
11/28/202012020487fghClaytonGAAtlantaSoutheastRUTE$6.98
11/28/2020120203902ghiMiltonWIMilwaukeeCentralRUTE$258.00
11/29/2020120201abcWaukeganILChicagoCentralRUTE$69.00
11/29/202012020349vwxColumbianaOHAkronNortheastRUTE$6.74
11/30/2020120204016mnoCaryILChicagoCentralRUTE$2.58
11/30/202012020349vwxColumbianaOHAkronNortheastRUTE$0.69
12/1/202012020412defParsonsKSJoplin-PittsburgCentralRUTP$29.47
12/1/202012020487fghClaytonGAAtlantaSoutheastRUTE$47.69
12/1/2020120203961jklGunnisonUTSalt Lake CityWestNRUTTech$68.30
12/2/2020120203902ghiMiltonWIMilwaukeeCentralRUTE$11.98
12/2/2020120201abcWaukeganILChicagoCentralRUTE$11.77
12/2/202012020412defParsonsKSJoplin-PittsburgCentralRUTP$9.60
12/3/202012020487fghClaytonGAAtlantaSoutheastRUTE$29.54
12/3/2020120204016mnoCaryILChicagoCentralRUTE$56.12
12/4/2020220203961jklGunnisonUTSalt Lake CityWestNRUTTech$40.96
12/4/202022020487fghClaytonGAAtlantaSoutheastRUTE$9.23
12/4/2020220203902ghiMiltonWIMilwaukeeCentralRUTE$143.95
12/4/202022020349vwxColumbianaOHAkronNortheastRUTE$0.18
12/4/202022020487fghClaytonGAAtlantaSoutheastRUTE$233.80

 

I want to find the sum of 'Amount' for each location per week. I'm skeptical about pivoting the table in data source as I have other tabs in the report which I don't want to mess around with. Is there a way to achieve it using a DAX query? Below is my expected result:

 

LocationNoLocationNameCityProvinceMarketRegionDivisionWeek 1 - 2019Week 1 - 2020Week 2 - 2019Week 2 - 2020
1abcWaukeganILChicagoCentralRUTE$252.11$380.77$312.00$0.00
349vwxColumbianaOHAkronNortheastRUTE$75.30$7.43$0.00$0.18
412defParsonsKSJoplin-PittsburgCentralRUTP$114.85$59.72$13.60$0.00
487fghClaytonGAAtlantaSoutheastRUTE$35.90$84.21$86.95$243.03
3902ghiMiltonWIMilwaukeeCentralRUTE$97.97$269.98$0.00$143.95
3961jklGunnisonUTSalt Lake CityWestNRUTTech$1,104.50$68.30$48.62$40.96
4016mnoCaryILChicagoCentralRUTE$135.02$65.68$0.00$0.00

 

I have tried creating measures for each week but I'm not getting the correct result. I'm providing one such measure:

Week1 = Var mon = 1
return
CALCULATE(sum(Query1[Amount]), CONVERT(Custom_Date[WeekNum],INTEGER)=mon, FILTER(Custom_Date,Custom_Date[Year]=[Year]))+0
1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

What you are looking for can be obtained just with a matrix visual with Year and Week on columns.  See pic below.  No measure was needed.  However, if you want to show 0s, you can make a simple SUM measure on that column and just add "+ 0".

 

Also, FYI that there is a simpler way to to make your Date tables vs. making 2019 and 2019 separately, and then appending.  If you do use that approach, FYI too that you can right click in the query editor and uncheck Enable Load so that tables not used in visuals/analysis are not loaded.  File attached too.

 

mahoneypat_0-1608208989278.png

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

9 REPLIES 9
mahoneypat
Microsoft Employee
Microsoft Employee

Just use this measure in the values area of the matrix

 

NewMeasure = SUM(Query1[Amt]) + 0





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

@mahoneypat Thank you so much for the solution! Could you please elaborate a little on how to show 0's? For example, in the above sample dataset, location number 349 has no amount in week 2 of 2019. What will be the new measure and on which column? I'm not quite understanding it well. Kindly help me!

mahoneypat
Microsoft Employee
Microsoft Employee

What you are looking for can be obtained just with a matrix visual with Year and Week on columns.  See pic below.  No measure was needed.  However, if you want to show 0s, you can make a simple SUM measure on that column and just add "+ 0".

 

Also, FYI that there is a simpler way to to make your Date tables vs. making 2019 and 2019 separately, and then appending.  If you do use that approach, FYI too that you can right click in the query editor and uncheck Enable Load so that tables not used in visuals/analysis are not loaded.  File attached too.

 

mahoneypat_0-1608208989278.png

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

@mahoneypat I'm providing a link to the https://drive.google.com/file/d/1B7fQBz_3XS4pL18cxilinPIXsxAIKQfJ/view?usp=sharing  sample file for your reference. The expected output is mentioned in my post description. Also, please note that I have applied filters in the 'ReportedTotalDate' column as I'm looking to evaluate only the last week of November and December. Also, I had to create 'Custom_Date' table in order to add a custom week number (WeekNum) column. 'Query1' and 'Query2' are fact tables in my sample dataset as well as in this sample dataset. Now, in order to find sum of amount per week for each location and in respective years, I had thought of creating 4 new measures, for example, if I want to create a measure for Week1-2019, it would be as following: Week1_2019 = var vThisValue = SELECTEDVALUE(WeekTable[Value]) return CALCULATE(SUM(Query1[Amount]), CONVERT(Custom_Date[WeekNum],INTEGER) = vThisValue,FILTER(Query1,Query1[FundraisingYear]=[Year]))+0 , but this is not returning any value. 

mahoneypat
Microsoft Employee
Microsoft Employee

@Anonymous  If you can reply with a link with your pbix (or a mock up of it), I can get it working.  I wasn't thinking you'd still do the CONVERT part.  Thanks for the suggestion @PaulDBrown 

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

@mahoneypat I'm not getting any value. It's just returning blank values. I'll explain you the steps that I have gone through so that you can guide me if I'm doing anything wrong at any step:

1. Created a cutom table and named it WeekTable using the formula GENERATESERIES(1,5)

2. Created a relationship between WeekTable and the other table named 'Custom_Date' which has week number (This table is also a custom made table). 

3. Since, I need to find the total amount per week for each location and separately for 2019 and 2020, I first tried creating a measure to find the sum of amount for 2020. For this, I have used the formula:

Weekly Sum = var vThisValue = SELECTEDVALUE(WeekTable[Value]) return CALCULATE(SUM(Query1[Amount]), CONVERT(Custom_Date[WeekNum],INTEGER) = vThisValue,FILTER(Query1,Query1[FundraisingYear]=[Year]))+0 

(Since, the week number is of 'text' data type, I had to use the convert function to change it into Integer).

 

I believe there's something wrong with CONVERT(Custom_Date[WeekNum],INTEGER).  If I don't use 'CONVERT', it gives me an error to convert text data type to integer.

mahoneypat
Microsoft Employee
Microsoft Employee

I wondered if that would happen, but was in a hurry.  Please try same but pull that into a variable first.

 

Weekly Sum = var vThisValue = SELECTEDVALUE(WeekTable[Value])
Return CALCULATE(SUM(Query1[Amt]), Query1[WeekNum] = vThisValue)

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

@mahoneypat It is throwing an error "A function 'SELECTEDVALUE' has been used in a True/False expression that is used as a table filter expression. This is not allowed".  As mentioned, column 'WeekNum' is not coming from Query1 (fact table) but from a custom made table 'Custom_date'. Also, how will I get the total sum of each location no rolled up to each week for 2019 and 2020? Kindly help me as I'm stuck in this issue. Any help is highly appreciated!

mahoneypat
Microsoft Employee
Microsoft Employee

You could add a disconnected table with GENERATESERIES(1,10) for example, use that column on columns in your matrix and then use the SELECTEDVALUE() of that new table column in your formula.  That way you need just one measure.

 

Weekly Sum = CALCULATE(SUM(Query1[Amt]), Query1[WeekNum] = SELECTEDVALUE(WeekTable[Value]))

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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