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
brief001
Helper II
Helper II

Calculated column with sum as function

I'm looking for a formula that uses sums as logic that we know in Excel. I want to add a calculated column (orange) in the table below. That sums values of column value if the column values of date and color are equal.

brief001_0-1668161934689.png


I once came across a formula with the EARLIER function, but I don't know the exact formula to get it to work.

1 ACCEPTED SOLUTION
daXtreme
Solution Sage
Solution Sage

Hint: You should never use EARLIER. There is a much better alternative and it's called VARIABLES.

// This is the formula for your column...

[Total Value] =
var CurrentDate = YourTable[Date]
var CurrentColor = YourTable[Color]
var Output =
    sumx(
        filter(
            YourTable,
            YourTable[Date] = CurrentDate
            &&
            YourTable[Color] = CurrentColor
        ),
        YourTable[Value]
    )
return
    Output

 

View solution in original post

5 REPLIES 5
brief001
Helper II
Helper II

I want to thank you both ( @daXtreme & @zerotyper  ) for the quick and well-functioning response. I have been helped tremendously.

As a result of @daXtreme 's additional explanation, I naturally opt for his solution.

Thanks and have a nice weekend!

zerotyper
Frequent Visitor

@brief001 Hope this is the result you want.

zerotyper_0-1668169111942.png

 

@zerotyper 

 

A word of caution.

 

Using CALCULATE in calculated column is a no-no. This slows DAX tremendously down even on moderate sized sets. I've seen many a time formulas like this that brought the engine down to a complete freeze.

@daXtreme Really thanks for your suggestion.

You are right, just use Variable and simply filter and Aggregation is better than Calculate in a calculated column.

The context transition is too heavy.

daXtreme
Solution Sage
Solution Sage

Hint: You should never use EARLIER. There is a much better alternative and it's called VARIABLES.

// This is the formula for your column...

[Total Value] =
var CurrentDate = YourTable[Date]
var CurrentColor = YourTable[Color]
var Output =
    sumx(
        filter(
            YourTable,
            YourTable[Date] = CurrentDate
            &&
            YourTable[Color] = CurrentColor
        ),
        YourTable[Value]
    )
return
    Output

 

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