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
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
v-rzhou-msft
Community Support
Community Support

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
v-rzhou-msft
Community Support
Community Support

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.

v-rzhou-msft
Community Support
Community Support

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
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.