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

How to create a dax function that works both in table context and in the context of Total?

Hello,

 

I am new to PowerBI. I hope someone can help as I've been struggling for a few hours.

 

Let's say I have the below data.

 

dateevent_labeltrials associated with eventstrials per day
2023-01-01label 15120
2023-01-01

label 2

15120
2023-01-01label 39120
2023-01-02label 26116
2023-01-02label 413116

 

In a table view I am pulling in the "date", "trials associated with events" and "trials per day" columns. PowerBI correctly decides to sum the column "trials associated with events".


Problem1 : It then incorrectly decides to sum the "trials per day" column. Not it's fault. This column is the result of an SQL window function that just outputs the number of trials as reported by internal systems per day, hence the same number is repeated for every row with the same date.

 

This was not so hard to circumvente at the row level. I can either take the average or min or max or median. I can even use the below dax function code to actually grab just the value in the first row in the group.

 

First value = FIRSTNONBLANK(

    Table[trials per day]

    , [trials per day] <> BLANK()

)

 

So many options. I am so smart.

 

Problem 2 (which I can't solve because I am not so smart): Where all of the above options fail is when it comes to the "Total" values at the bottom of the table view. I am looking for a way to get a total that is the sum of all those first values from the column "trials per day" when grouping by date by "date".

 

So basically the below table is what I want to see in the view.

 datetrials associated with eventstrials per day
 2023-01-0129120
 2023-01-0219116
Totals 48236

 

I've been playing around with context manipulation but no luck.

 

Kind regards,

Cristian

1 ACCEPTED SOLUTION
ppm1
Solution Sage
Solution Sage

Please see this video for how to get your expected total.

(2) Power BI - Tales from the front #01 - Getting the Right Total - YouTube

 

In your case, you likely need something like

 

NewMeaure = SUMX(VALUES(Table[Date]), [Trials associated with events])

 

Pat

 

Microsoft Employee

View solution in original post

6 REPLIES 6
djurecicK2
Super User
Super User

Hi @cristiancapsuna,

 Glad that helped. I'm not sure I understand the second issue- can you please explain what is happening now and what you are looking for?

 

 

ppm1
Solution Sage
Solution Sage

Please see this video for how to get your expected total.

(2) Power BI - Tales from the front #01 - Getting the Right Total - YouTube

 

In your case, you likely need something like

 

NewMeaure = SUMX(VALUES(Table[Date]), [Trials associated with events])

 

Pat

 

Microsoft Employee

Hi Pat. Thank you for your help. I modified it to the below and it worked. And it workes by itself without separating the row level from the total level.

SUMX(SUMMARIZE(Table,[date], [trials per day]), [trials per day])
I will check out your youtube channel when I have a bit of time to pick up some more knowledge.
cristiancapsuna
New Member

Hi @djurecicK2 ,

 

Thank you for your response. That gives me a very nice way to separate the row level from the total overview. I am still stuck on how to apply the date group by context on the contextless Total. Would you be able to help me with that? If you edit your last message then I can mark it as having solved the entire problem.

djurecicK2
Super User
Super User

Hi @cristiancapsuna ,

 You can use IF(HASONEVALUE(tablename[date]) to determine if the row is a normal row with a date or a total row. If it has one value it is a normal row, if it doesn't it is a total row.

https://mitchellpearson.com/2018/05/18/unexpected-totals-in-dax-part-1/

 

Hi @djurecicK2 . Thanks for teaching me about the HASONEVALUE function. I will keep that in the back pocket. On this ocassion it seems that what Pat suggested handles the context correctly by itself so I'll go with that as it's simpler.

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.