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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
rsanyoto
Helper III
Helper III

Calculate variable with if statement based on date without value

Hello everyone,

Im kinda new to the DAX and im struggleing to calculate the final cost price of a product based on some variables.

 

What i have fixed is following:

  1. So if there is a transaction that has a posting date that has a value between startingdate and endingdate then the Variable CostPriceWithDate will solve this.

  2. The final cost price for a product with a date is calculated in variable FinalCostPriceWithDate . 

  3. Ifthere is no value on the column  (Dim_PurchasePrice[EndingDate])  (see the Variable NoDate) then calculate the variable CostPriceWithoutDate. The final cost price for a product without a date is calculated in variable FinalCostPriceWithoutDate.

 

The requirement is: if there is no value in the column Ending Date (see image below) then use the variable FinalCostPriceWithoutDate , otherwise use the variable FinalCostPriceWithDate .

 

 

My question is how to return the correct value (based on the underneath Dax) if there is no data in a column Ending Date (see image below)  I think the part of Var NoDate and my Return statement is not correct. Please help. 

 

Kind regards 

 

price without ending date.png

 

 

Totaal  CostPrice Products =

VAR Date =

    MAX ( Fact_ValueEntry[PostingDate] )

VAR NoDate =

MIN (Dim_PurchasePrice[EndingDate])

 

VAR Product =

    MAX ( Fact_ValueEntry[ItemFK] )

 

VAR CostPriceWithDate =

    CALCULATE (

        MAX ( Dim_PurchasePrice[DirectUnitCost] ),

        Dim_PurchasePrice[ItemFK] = Product,

        Dim_PurchasePrice[StartingDate] < Date,

        Dim_PurchasePrice[EndingDate] >= Date

    )

VAR CostPriceWithoutDate =

    CALCULATE (

        MAX ( Dim_PurchasePrice[DirectUnitCost] ),

        Dim_PurchasePrice[ItemFK] = Product,

        Dim_PurchasePrice[StartingDate] < Date,

        Dim_PurchasePrice[EndingDate] = NoDate)

 

VAR CostPriceInk =

    CALCULATE (

        max ( Dim_PurchasePrice[DirectUnitCost] ),

        Dim_PurchasePrice[ItemFK] = "INK")

 

VAR CostPriceGlue =

    CALCULATE (

        max ( Dim_PurchasePrice[DirectUnitCost] ),

        Dim_PurchasePrice[ItemFK] = "GLUE")

 

VAR FinalCostPriceWithDate = CostPriceWithDate + CostPriceInk + CostPriceGlue

VAR FinalCostPriceWithoutDate = CostPriceWithoutDate + CostPriceInk + CostPriceGlue

 

RETURN

  IF(ISBLANK(NoDate), FinalCostPriceWithoutDate, FinalCostPriceWithDate))

 

1 ACCEPTED SOLUTION
rsanyoto
Helper III
Helper III

hi everyone & @HotChilli,
 
My collegeau and i have solved the issue for the requirement. the main root cause is the data type of the column EndingDate which has records with value 1753/01/01. After initial load to Power BI file, this record is treated as a date. This created a mismatched with my variables in the measure.  So i changed in my SQL view with nullif statement to make the records as BLANK. 
 
 The following dax code works :
 
Total CostPrice Products =

VAR WithDate =
MAX ( Fact_ValueEntry[PostingDate] )

VAR NoDate =
MIN (Dim_PurchasePrice[EndingDate])


VAR Product =
MAX ( Fact_ValueEntry[ItemFK] )


VAR StartingDatePurchase =
CALCULATE(MAX(Dim_PurchasePrice[StartingDate]),
Dim_PurchasePrice[StartingDate] <= WithDate)

VAR CostPriceWithDate =
CALCULATE (

MAX ( Dim_PurchasePrice[DirectUnitCost] ),

Dim_PurchasePrice[ItemFK] = Product,

Dim_PurchasePrice[StartingDate] = StartingDatePurchase,

NOT( Dim_PurchasePrice[ItemFK] IN {"INKT","GLUE"})

)
 
VAR CostPriceInk =

CALCULATE (

MAX ( Dim_PurchasePrice[DirectUnitCost] ),

Dim_PurchasePrice[ItemFK] = "INKT")

 
VAR CostPriceGlue =

CALCULATE (

max ( Dim_PurchasePrice[DirectUnitCost] ),

Dim_PurchasePrice[ItemFK] = "LIJM")

RETURN

CostPriceWithDate + CostPriceInk + CostPriceGlue

View solution in original post

4 REPLIES 4
v-henryk-mstf
Community Support
Community Support

Hi @rsanyoto ,

 

Very happy about your feedback.

 

If the problem has been solved, you can mark the correct response as the standard answer to help the other members find it more quickly. Looking forward to your reply.


Best Regards,
Henry

 

rsanyoto
Helper III
Helper III

hi everyone & @HotChilli,
 
My collegeau and i have solved the issue for the requirement. the main root cause is the data type of the column EndingDate which has records with value 1753/01/01. After initial load to Power BI file, this record is treated as a date. This created a mismatched with my variables in the measure.  So i changed in my SQL view with nullif statement to make the records as BLANK. 
 
 The following dax code works :
 
Total CostPrice Products =

VAR WithDate =
MAX ( Fact_ValueEntry[PostingDate] )

VAR NoDate =
MIN (Dim_PurchasePrice[EndingDate])


VAR Product =
MAX ( Fact_ValueEntry[ItemFK] )


VAR StartingDatePurchase =
CALCULATE(MAX(Dim_PurchasePrice[StartingDate]),
Dim_PurchasePrice[StartingDate] <= WithDate)

VAR CostPriceWithDate =
CALCULATE (

MAX ( Dim_PurchasePrice[DirectUnitCost] ),

Dim_PurchasePrice[ItemFK] = Product,

Dim_PurchasePrice[StartingDate] = StartingDatePurchase,

NOT( Dim_PurchasePrice[ItemFK] IN {"INKT","GLUE"})

)
 
VAR CostPriceInk =

CALCULATE (

MAX ( Dim_PurchasePrice[DirectUnitCost] ),

Dim_PurchasePrice[ItemFK] = "INKT")

 
VAR CostPriceGlue =

CALCULATE (

max ( Dim_PurchasePrice[DirectUnitCost] ),

Dim_PurchasePrice[ItemFK] = "LIJM")

RETURN

CostPriceWithDate + CostPriceInk + CostPriceGlue
HotChilli
Super User
Super User

Can you provide a test pbix please because it's a bit difficult to debug this without seeing the model/relationships?

Just fake a small example (and provide the desired result please)   and link it from an external site and I'll have a look for you

Hi @HotChilli ,

 

I have uploaded the pbix example in my public github repository. Download the file of 2,19 mb

 

Please try this link:

https://github.com/regazzi24/powerbi/blob/main/Example%2011102021%20v1.pbix

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.