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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
christinaxxx
Helper I
Helper I

Sum of a DIVIDE, then divided by DISTINCTCOUNT, then get the average across user ID column

Hi, I'm trying to write a complex DAX to achive a visualisation on Truck Profitability but can't seem to work it out. 

For each Truck ID, I want to get the Gross Profit per Day. Say Truck A, it will be 15/5 = 3.
Then, I want to sum the above up to each User ID and divide that by the distinct count of Truck ID of that user to get the Truck Profitability for each user. So for User A, it will be (3+2)/2 = 2.5

The DAX should also be able to show national average (i.e., average of user A & B) when put in a table visual.

 

The hard part is that not all trucks go out every day within a period (e.g., a month so 30 days) so I have to aggregate the value bottom up by considering the number of days out for each.

 

User IDTruck IDDelivery DateGross Profit
ATruck A1/01/20241
ATruck A2/01/20242
ATruck A3/01/20243
ATruck A4/01/20244
ATruck A5/01/20245
ATruck B4/01/20241
ATruck B5/01/20242
ATruck B6/01/20243
BTruck C1/01/20241
BTruck C2/01/20242
BTruck D3/01/20243
BTruck D4/01/20244
1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1706591259438.png

 

Jihwan_Kim_1-1706592197513.png

 

Expected result measure: =
VAR _grossprofit =
    ADDCOLUMNS (
        SUMMARIZE ( Data, Truck[Truck ID] ),
        "@grossprofit", CALCULATE ( SUM ( Data[Gross Profit] ) ),
        "@daycount", CALCULATE ( COUNTROWS ( SUMMARIZE ( Data, 'Calendar'[Date] ) ) )
    )
VAR _dailyavg =
    ADDCOLUMNS (
        _grossprofit,
        "@dailyavg", DIVIDE ( [@grossprofit], [@daycount] ),
        "@truckcount", CALCULATE ( COUNTROWS ( SUMMARIZE ( Data, Truck[Truck ID] ) ) )
    )
VAR _result =
    AVERAGEX ( _dailyavg, DIVIDE ( [@dailyavg], [@truckcount] ) )
RETURN
    _result

 

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

View solution in original post

3 REPLIES 3
123abc
Super User
Super User

To achieve the desired calculation of Truck Profitability for each user, you need to break down the problem into steps. You'll need to calculate the Gross Profit per Day for each truck, then sum it up for each user, and finally divide by the distinct count of Truck IDs for that user. Here's how you can do it in DAX:

  1. Calculate Gross Profit per Day per Truck: You need to create a measure that calculates the Gross Profit per Day for each truck. This can be done by dividing the total Gross Profit for each truck by the count of distinct delivery dates for that truck.

Gross Profit per Day =
VAR TotalProfit = SUM('Table'[Gross Profit])
VAR DistinctDays = DISTINCTCOUNT('Table'[Delivery Date])
RETURN
DIVIDE(TotalProfit, DistinctDays, 0)

 

Calculate Truck Profitability for Each User: Now, you'll sum up the Gross Profit per Day for each user and then divide it by the distinct count of Truck IDs for that user.

 

Truck Profitability =
VAR TotalProfitPerUser =
SUMX(
VALUES('Table'[User ID]),
[Gross Profit per Day]
)
VAR DistinctTrucksPerUser = DISTINCTCOUNT('Table'[Truck ID])
RETURN
DIVIDE(TotalProfitPerUser, DistinctTrucksPerUser, 0)

 

  1. Calculate National Average: To calculate the national average, you can directly use the Truck Profitability measure.

Now, you can use the Truck Profitability measure in your table visual, and it should give you the desired results. Make sure to adjust the table visuals and slicers as needed to display the data properly.

These measures assume that you have a table named 'Table' containing the columns User ID, Truck ID, Delivery Date, and Gross Profit. Adjust the column names accordingly if they are different in your dataset.

 
 
 
 

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1706591259438.png

 

Jihwan_Kim_1-1706592197513.png

 

Expected result measure: =
VAR _grossprofit =
    ADDCOLUMNS (
        SUMMARIZE ( Data, Truck[Truck ID] ),
        "@grossprofit", CALCULATE ( SUM ( Data[Gross Profit] ) ),
        "@daycount", CALCULATE ( COUNTROWS ( SUMMARIZE ( Data, 'Calendar'[Date] ) ) )
    )
VAR _dailyavg =
    ADDCOLUMNS (
        _grossprofit,
        "@dailyavg", DIVIDE ( [@grossprofit], [@daycount] ),
        "@truckcount", CALCULATE ( COUNTROWS ( SUMMARIZE ( Data, Truck[Truck ID] ) ) )
    )
VAR _result =
    AVERAGEX ( _dailyavg, DIVIDE ( [@dailyavg], [@truckcount] ) )
RETURN
    _result

 

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Thank you. This worked perfectly

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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