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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
czaldumbide
Helper I
Helper I

calculating sum for max date

I created a matrix with the product name as rows, and the sum of amount for the latest purchase date as the value. 

 

I am using the following measure for my value: 

CALCULATE(SUM('Sales'[Amount]), FILTER ('Sales', 'Sales'[Date] = MAX('Sales'[Date] )))

 

Even though the matrix displays the correct grand total in the table, I still get product names that do not have a record for the latest date of my table, and instead of showing a BLANK value they display the SUM('Sales'[Amount]) that belongs to the latest date that product appeared in the table.

 

Lets say my latest date of the table is 11/05/2020. Let me modify my measure to be the following:

CALCULATE(SUM('Transactions'[Amount]), FILTER ('Transactions', 'Transactions'[Date] = DATE(2020,05,11))

 

This measure does give me the right matrix, but I want my  latest date to be automated. Can anyone help me understand why is this happening and what I can do to fix it. 

 

Thank you!

 

2 ACCEPTED SOLUTIONS
AlB
Super User
Super User

Hi @czaldumbide 

The MAX() in your measure will give you the latest date for the product in that row, since products is part of the filter context. Try this:

Measure =
VAR latestdate_ =
    MAX ( ALL ( 'Sales'[Date] ) )
RETURN
    CALCULATE (
        SUM ( 'Sales'[Amount] ),
        FILTER ( 'Sales', 'Sales'[Date] = latestdate_ )
    )

 Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs

Cheers 

SU18_powerbi_badge

View solution in original post

Anonymous
Not applicable

There are several things wrong with your code.

 

First, you are creating a filter out of the whole expanded fact table. THIS IS VERY, VERY BAD and slows down calculations tremendously. If you start doing such calculations on big fact tables, you'll feel the heat instantly. One of the golden rules of DAX programming says: Never filter a table if you can filter a column.

 

Secondly, you should have a Calendar dimension in your model.

 

If you do have a proper Calendar, then you can write:

[Latest Sales Amount] =
var __latestSalesDate =
	CALCULATE(
		// This is the very latest
		// day with any sales in it
		// with no regard to any
		// selections in any slicers.
		MAX( Sales[Date] ),
		ALL( Sales )
	)
var __result = 
	CALCULATE(
		SUM( Sales[Amount] ),
		// This only works correctly
		// if Calendar is THE date table
		// in the model.
		Calendar[Date] = __latestSalesDate
	)
return
	__result


Best
D

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

@amitchandak @AlB 

 

I have a similar situation for aggregated column. How to create it? Please help

 

Measure =
VAR latestdate_ =
MAX ( ALL ( 'Sales'[Date] ) )
RETURN
CALCULATE (
SUM ( 'Sales'[Amount] ),
FILTER ( 'Sales', 'Sales'[Date] = latestdate_ )

 

Anonymous
Not applicable

There are several things wrong with your code.

 

First, you are creating a filter out of the whole expanded fact table. THIS IS VERY, VERY BAD and slows down calculations tremendously. If you start doing such calculations on big fact tables, you'll feel the heat instantly. One of the golden rules of DAX programming says: Never filter a table if you can filter a column.

 

Secondly, you should have a Calendar dimension in your model.

 

If you do have a proper Calendar, then you can write:

[Latest Sales Amount] =
var __latestSalesDate =
	CALCULATE(
		// This is the very latest
		// day with any sales in it
		// with no regard to any
		// selections in any slicers.
		MAX( Sales[Date] ),
		ALL( Sales )
	)
var __result = 
	CALCULATE(
		SUM( Sales[Amount] ),
		// This only works correctly
		// if Calendar is THE date table
		// in the model.
		Calendar[Date] = __latestSalesDate
	)
return
	__result


Best
D

hi,  CALCULATE(SUM('Sales'[Amount]), FILTER ('Sales', 'Sales'[Date] = MAX('Sales'[Date] ))) 

the same metric, wen i pulled in , it is blank and it needs always date column to b pulled in along wit it , to get the data. Is there a way to populate the amount for max date without pulling the datekey in the report rows?

Anonymous
Not applicable

Hi @czaldumbide ,

 

Measure =
VAR latestdate_ =
    CALCULATE (
        MAX ( 'Sales'[Date] ),
        ALLSELECTED ( 'Sales' ),
        VALUES ( 'Sales'[product] )
    )
RETURN
    CALCULATE (
        SUM ( 'Sales'[Amount] ),
        FILTER ( 'Sales', 'Sales'[Date] = latestdate_ )
    )

 

amitchandak
Super User
Super User

@czaldumbide , try like

measure =
var _max = MAXX('Sales','Sales'[Date] )
return
CALCULATE(SUM('Sales'[Amount]), FILTER ('Sales', 'Sales'[Date] =_max ))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
AlB
Super User
Super User

Hi @czaldumbide 

The MAX() in your measure will give you the latest date for the product in that row, since products is part of the filter context. Try this:

Measure =
VAR latestdate_ =
    MAX ( ALL ( 'Sales'[Date] ) )
RETURN
    CALCULATE (
        SUM ( 'Sales'[Amount] ),
        FILTER ( 'Sales', 'Sales'[Date] = latestdate_ )
    )

 Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs

Cheers 

SU18_powerbi_badge

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.