Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

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!

  • Anonymous's avatar
    Anonymous
    6 years ago

    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. 

5 Replies

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

  • Anonymous's avatar
    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. 

  • Anonymous's avatar
    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's avatar
      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()

      • lbendlin's avatar
        lbendlin
        Icon for Super User rankSuper User

        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.