cancel
Showing results for
Did you mean:

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Anonymous
Not applicable

## 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.

 date event_label trials associated with events trials per day 2023-01-01 label 1 5 120 2023-01-01 label 2 15 120 2023-01-01 label 3 9 120 2023-01-02 label 2 6 116 2023-01-02 label 4 13 116

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.

 date trials associated with events trials per day 2023-01-01 29 120 2023-01-02 19 116 Totals 48 236

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

Kind regards,

Cristian

1 ACCEPTED SOLUTION
Solution Sage

(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
6 REPLIES 6
Super User

Hi @Anonymous,

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?

Solution Sage

(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
Anonymous
Not applicable

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.
Anonymous
Not applicable

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.

Super User

Hi @Anonymous ,

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/

Anonymous
Not applicable

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.

Announcements

#### Fabric certifications survey

Certification feedback opportunity for the community.

#### Power BI Monthly Update - April 2024

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

#### Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors