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
hcze
Helper I
Helper I

DAX for column total

Hi,

 

I want to have the column total as the first column in a matrix but the total does not show up (blank) for some reasons.

 

As a background, there are three tables involved: TblCategory, TblYear, Facts which have relationship amongst them. There also slicers for incoming filters,

 

My current DAX is

Amount  =
VAR CY = SELECTEDVALUE(TblYear[Sales Year])
RETURN
IF (CY = "All Years",
SUM(Facts[Sales]),
CALCULATE(SUM(Facts[SalesI]),TblYear[Sales Year]=CY)
)

 

Following is the expected result (and it needs to take into account slicers as well)

CategoryAll Years202020192018
Books29010080110
Clothes590200210180
Toys30012010080

 

TblYear

2018
2019
2020
All Years

 

 

TblCategory

Books
Clothes
Toys

 

 

Facts

Books2018110
Books201980
Books2020100
Clothes2018180
Clothes2019210
Clothes2020200
Toys201880
Toys2019100
Toys2020120

 

Thanks for helping!

 

 

 

 

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

Here is how you can do this one.

 

First, add an index to your TblYear table so you can use it as a Sort By Column to get All Years to show before the years.

 

Then use this measure.

 

NewMeasure =
VAR thisyeartotal =
SUM ( Facts[Amount] )
RETURN
IF (
ISBLANK ( thisyeartotal ),
CALCULATE ( SUM ( Facts[Amount] ), ALL ( TblYear ) ),
thisyeartotal
)

 

 

mahoneypat_0-1598404552736.png

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

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

6 REPLIES 6
mahoneypat
Employee
Employee

Here is how you can do this one.

 

First, add an index to your TblYear table so you can use it as a Sort By Column to get All Years to show before the years.

 

Then use this measure.

 

NewMeasure =
VAR thisyeartotal =
SUM ( Facts[Amount] )
RETURN
IF (
ISBLANK ( thisyeartotal ),
CALCULATE ( SUM ( Facts[Amount] ), ALL ( TblYear ) ),
thisyeartotal
)

 

 

mahoneypat_0-1598404552736.png

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

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


@mahoneypat That's brilliant! Thanks!

harshnathani
Community Champion
Community Champion

HI @hcze ,

 

 

You can remove 'All Years' from your Year Table.

 

This is how your model will look.

 

1.jpg

 

In the slicer you have an option to Select All. Use that option incase you want to get the values of All Years.

 

 

 

2.JPG

 

 

Use values from the different tables to show in a Matrix.

 

 

3.JPG

 

Regards,
Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

@harshnathani  it was to have  total column as first column. Unless you have other trick to get there?

 

amitchandak
Super User
Super User

@hcze ,If TblYear is connected to Facts then it will filter automatically in this case. If not filter need be move fact[Year] or year(fact[date])

 

example


Amount =
VAR CY = SELECTEDVALUE(TblYear[Sales Year])
RETURN
IF ( not(isfiltered(TblYear[Sales Year])),
SUM(Facts[Sales]),
CALCULATE(SUM(Facts[SalesI]),filter(fact, year(fact[date]) =CY))
)

 

isfiltered can be used to check if there any value selected or not

https://powerpivotpro.com/2013/03/hasonevalue-vs-isfiltered-vs-hasonefilter/

Greg_Deckler
Super User
Super User

@hcze - Difficult to get a handle on exactly what is wrong without source data. But, seems like you are trying to add a measure into your matrix and so you may need The New Hotness - https://community.powerbi.com/t5/Quick-Measures-Gallery/The-New-Hotness-Custom-Matrix-Hierarchy/m-p/...

 

But, hard to say, Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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