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
jpmakako
Frequent Visitor

Trouble with Calculated Column with Dates

Hi, I have two tables. One with Inventory of "Units" (and their respective "Projects"), "Price Unit" and "When Date" the "Units" were sold, the other table "DatePrices" shows the dates with different prices for different dates to each of this unit. Im trying to bring the Value each "Unit" should have been priced by looking at the oldest list price {less or equal to} "When Date" it was sold. I have the following DAX in the inventory table trying to bring the Date of the list the column is bringing the value from:

 

Latest Date = VAR Proy = [Projects] VAR Unit = [Units] VAR DateS = [When Date]
VAR MaxDate =
    CALCULATE(
        MAX(DatePrices[Date]),
        DatePrices[Unit] = Unit,
        DatePrices[Project] = Proy,
        DatePrices[Date] <= DateS      
    )
RETURN
    MaxDate
 
and 
 
Should Price = VAR Proy = [Projects] VAR Unit = [Units] VAR DateS = [When Date]
VAR MaxDate =
    CALCULATE(
        MAX(DatePrices[Date]),
        DatePrices[Unit] = Unit,
        DatePrices[Project] = Proy,
        DatePrices[Date] <= DateS      
    )
RETURN
CALCULATE(
       
CALCULATE(
        SUM(DatePrices[Price]),
        DatePrices[Unit] = Unit,
        DatePrices[Project] = Proy,
        DatePrices[Date] = Max Date       
    )
The problem is the value in the columns only works when the value of "When Date" is exactly the same as the DatePrices[Date]. As if the "<=" was "=". I thought it was comparing the date as text when going through the Var DateS and tried DatePrices[Date] <= Date(Year(DateS),Month(DateS),Day(DatesS) but the results are the same. Any ideas what Im missing?
 
[I tried  EVALUATE
//{CALCULATE( MAX(DatePrices[Date]),
FILTER(DatePrices,
        DatePrices[Unit] = "1001-2" && 
        DatePrices[Project] = "Praia"  
        && DatePrices[Date] <= DATE(2023,3,22)      
    )//)} in Dax Studio and works as it should]     
1 ACCEPTED SOLUTION
jpmakako
Frequent Visitor

I used this instead and worked, no idea why.

 

Should Price = VAR Proy = [Projects] VAR Unit = [Units] VAR DateS = [When Date]
VAR MaxDate =
    MAXX(FILTER( DatePrices,
        DatePrices[Unit] = Unit &&
        DatePrices[Project] = Proy &&
        DatePrices[Date] <= DateS)
    , DatePrices[Date]
    )
RETURN
    SUMX(FILTER( DatePrices,
        DatePrices[Unit] = Unit &&
        DatePrices[Project] = Proy &&
        DatePrices[Date] = MaxDate)
    , DatePrices[Price]
    )

View solution in original post

2 REPLIES 2
jpmakako
Frequent Visitor

I used this instead and worked, no idea why.

 

Should Price = VAR Proy = [Projects] VAR Unit = [Units] VAR DateS = [When Date]
VAR MaxDate =
    MAXX(FILTER( DatePrices,
        DatePrices[Unit] = Unit &&
        DatePrices[Project] = Proy &&
        DatePrices[Date] <= DateS)
    , DatePrices[Date]
    )
RETURN
    SUMX(FILTER( DatePrices,
        DatePrices[Unit] = Unit &&
        DatePrices[Project] = Proy &&
        DatePrices[Date] = MaxDate)
    , DatePrices[Price]
    )
Wilson_
Super User
Super User

Hello jpmakako,

 

I would be happy to try to help. Can you share a sample pbix file?




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





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!

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.