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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Calculated Column's formula is based on a value in a column

Sorry if the subject is really vague, but what I just want to know if this is possible in Power BI. Let's say I have this table:

CountryCreated DateAssigned DateCompleted DateClosed DateDate Difference
PhilippinesXXXXXXXXXXXX 
USAXXXXXXXXXXXX 
ThailandXXXXXXXXXXXX 

 

Then the column "Date Difference" above would be a calculated column in which the calculation varies and I will have another table to indicated what formula and columns are involved in the calculation, let's say another table like this:

 

CountryDate Difference Calculation
PhillippinesClosed Date - Created Date
USACompleted Date - Created Date
Thailand

Closed Date - Assigned Date

 

You may think that I could have this be type out as a Dax formula as they just only involved 3 countries with a simple If and else statement but my problem is that it involves hundred of countries and typing it out is near to impossible. Would you know how to convert a calculation based on a column values to be a calculated column in Power BI?

 

Thank you very much and have a great day!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous 

You can't use the formula in columns, but you can use switch function like  

In addition to  

 

Date Difference = 
VAR _DateDifferenceCalucation = CALCULATE(MAX('Calculation'[Date Difference Calculation]),FILTER('Calculation','Calculation'[Country]=EARLIER('Table'[Country])))
VAR _Closed_Created =
    DATEDIFF ( 'Table'[Created Date], 'Table'[Closed Date], DAY )
VAR _Closed_Assigned =
    DATEDIFF ( 'Table'[Assigned Date], 'Table'[Closed Date], DAY )
VAR _Closed_Completed =
    DATEDIFF ( 'Table'[Completed Date], 'Table'[Closed Date], DAY )
VAR _Completed_Assigned =
    DATEDIFF ( 'Table'[Assigned Date], 'Table'[Completed Date], DAY )
VAR __Completed_Created =
    DATEDIFF ( 'Table'[Created Date], 'Table'[Completed Date], DAY )
RETURN
    SWITCH (
        _DateDifferenceCalucation,
        "Closed Date - Created Date", _Closed_Created,
        "Completed Date - Created Date", __Completed_Created,
        "Closed Date - Assigned Date", _Closed_Assigned,
        "Completed Date - Assigned Date", _Completed_Assigned,
        _Closed_Completed
    )

 

You can download the pbix file from this link: Calculated Column's formula is based on a value in a column

 

Best Regards,

Rico Zhou

 

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

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Hi @Anonymous 

Could you tell me if your problem has been solved? If it is, kindly Accept it as the solution. More people will benefit from it. Or you are still confused about it, please provide me with more details about your table and your problem or share me with your pbix file from your Onedrive for Business.

 

Best Regards,

Rico Zhou

Anonymous
Not applicable

I get the solution but I am having 2nd thoughts on that one as I am expecting that there would be hundreds of different calculations and doing that method will require me to input all the calculations. I was thinking more of using the Expression.evaluate()

Yes, go do that.  Have a table with country, date A and date B  column names, and then use Expression.Evaluate() to run the dynamic formula.  Column names need to be encapsulated in brackets  (eg [Created Date])  which makes your original format less usable.

Anonymous
Not applicable

Hi @Anonymous 

You can't use the formula in columns, but you can use switch function like  

In addition to  

 

Date Difference = 
VAR _DateDifferenceCalucation = CALCULATE(MAX('Calculation'[Date Difference Calculation]),FILTER('Calculation','Calculation'[Country]=EARLIER('Table'[Country])))
VAR _Closed_Created =
    DATEDIFF ( 'Table'[Created Date], 'Table'[Closed Date], DAY )
VAR _Closed_Assigned =
    DATEDIFF ( 'Table'[Assigned Date], 'Table'[Closed Date], DAY )
VAR _Closed_Completed =
    DATEDIFF ( 'Table'[Completed Date], 'Table'[Closed Date], DAY )
VAR _Completed_Assigned =
    DATEDIFF ( 'Table'[Assigned Date], 'Table'[Completed Date], DAY )
VAR __Completed_Created =
    DATEDIFF ( 'Table'[Created Date], 'Table'[Completed Date], DAY )
RETURN
    SWITCH (
        _DateDifferenceCalucation,
        "Closed Date - Created Date", _Closed_Created,
        "Completed Date - Created Date", __Completed_Created,
        "Closed Date - Assigned Date", _Closed_Assigned,
        "Completed Date - Assigned Date", _Completed_Assigned,
        _Closed_Completed
    )

 

You can download the pbix file from this link: Calculated Column's formula is based on a value in a column

 

Best Regards,

Rico Zhou

 

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

lbendlin
Super User
Super User

Yes, you can do that in DAX, sort of, but not in a column.

 

There is no EVALUATE() function in Power BI DAX (sadly!) so you have to resort to a dynamic measure:

 

Result := SWITCH([Date Difference Calculation],"Closed Date - Created Date",<formula here>,"Completed Date - Created Date",<formula here>,<alternative formula>)

 

Having said that there is an Expression.Evaluate() option in Power Query where you can actually do that to a column.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.