Forum Discussion

Haydos's avatar
Haydos
Advocate I
1 year ago
Solved

Dynamic Status Calculation

Hi everyone,

I'm fairly new to Power BI, currently migrating from Looker. I'm facing a challenge in trying to determine the status of sales dynamically based on a user-selected reporting date. Here is how the calculation looks in LookML:

CASE
WHEN "cancellation_date" <= "reporting_period_end_date" THEN 'Cancelled'

WHEN "settled_date" <= "reporting_period_end_date" THEN 'Settled'
WHEN "contract_issued_date" <= "reporting_period_end_date" THEN 'Contract Issued'
WHEN "deposit_date" <= "reporting_period_end_date" THEN 'Deposit'
ELSE 'N/A'
END

In Power BI, I have the [Reporting Period End Date] calculating based on a slicer selection of 'Date'[Date]. My current measure calculation is this:

Record Status =
VAR SelectedDate = ([Reporting Period End])  -- The selected Reporting Period End Date from slicer
VAR CancelledDate = MAXX('Sales', [Contract Cancelled Date])
VAR SettlementDate = MAXX('Sales', [Actual Settlement Date])
VAR ContractIssuedDate = MAXX('Sales', [Contract Issued Date])
VAR SoldDate = MAXX('Sales', [Sold Date]
RETURN
    SWITCH(
        TRUE(),
        CancelledDate <= SelectedDate && NOT ISBLANK(CancelledDate), "Cancelled",
        SettlementDate <= SelectedDate && NOT ISBLANK(SettlementDate), "Settled",
        ContractIssuedDate <= SelectedDate && NOT ISBLANK(ContractIssuedDate), "Contract Issued",
        SoldDate <= SelectedDate && NOT ISBLANK(SoldDate), "Sold",
        "N/A"
)
This seems to work well if I add it to a visual along with sufficient row-level detail (e.g. adding the customer's name shows the correct status). However, what I'd like to do is list each of the four statuses and provide a count of the rows within each. When I remove the customer name though, only "Cancelled" appears as the status with all rows counting under it. 
Any suggestions on how I can accomplish this? 
 
Below is some randomly generated sample data. The last column is what I'd expect to see, given a Reporting Date of 2025-01-31
CustomerSoldDateContractIssuedDateSettlementDateCancelledDateStatus as at 31-Jan-25
Murphy Collier3/06/202423/06/2024  Contract Issued
Edison Fleming22/04/202422/01/2025 6/02/2025Contract Issued
Fatima Meza    N/A
Lucian Walsh14/08/2024   Sold
Leia Preston13/11/2024  14/02/2025Sold
Vincenzo Gilbert25/03/2024  31/01/2025Cancelled
Jocelyn Jefferson12/12/202422/01/20253/02/2025 Contract Issued
Raylan Guzman24/02/20243/11/20249/01/2025 Settled
Ashley Andrade27/03/202413/01/2025 26/01/2025Cancelled
Abdiel Giles8/10/202415/01/202518/02/2025 Contract Issued
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi Haydos,

    I modify the formula to get each status based on current date fields and merge them to show as text.

    Record Status = 
    VAR SelectedDate =
        MAX ( DateTable[Date] )
    VAR CancelledDate =
        MAX ( T1[CancelledDate] )
    VAR SettlementDate =
        MAX ( T1[SettlementDate] )
    VAR ContractIssuedDate =
        MAX ( T1[ContractIssuedDate] )
    VAR SoldDate =
        MAX ( T1[SoldDate] )
    VAR isCancelled =
        IF (
            CancelledDate <= SelectedDate
                && NOT ISBLANK ( CancelledDate ),
            "Cancelled"
        )
    VAR isSettled =
        IF (
            SettlementDate <= SelectedDate
                && NOT ISBLANK ( SettlementDate ),
            "Settled"
        )
    VAR isContractIssued =
        IF (
            ContractIssuedDate <= SelectedDate
                && NOT ISBLANK ( ContractIssuedDate ),
            "Contract Issued"
        )
    VAR isSold =
        IF ( SoldDate <= SelectedDate && NOT ISBLANK ( SoldDate ), "Sold" )
    VAR merged =
        IF ( isCancelled <> BLANK (), isCancelled & ";" )
            & IF ( isSettled <> BLANK (), isSettled & ";" )
            & IF ( isContractIssued <> BLANK (), isContractIssued & ";" )
            & IF ( isSold <> BLANK (), isSold )
    RETURN
        IF ( merged <> BLANK (), merged, "N/A" )

    Then you can add a new table with all status types and use it as category of the table visual and write a measure formula to count correspond status from the concatenated text.

    Status Count = 
    VAR currStatus =
        SELECTEDVALUE ( NewTable[Status] )
    VAR summary =
        SUMMARIZE (
            ALLSELECTED ( T1 ),
            [Customer],
            [SoldDate],
            [ContractIssuedDate],
            [SettlementDate],
            [CancelledDate],
            "Status", [Record Status]
        )
    RETURN
        COUNTROWS (
            FILTER ( summary, SEARCH ( currStatus, [Record Status], 1, -1 ) > 0 )
        )


    Regards,

    Xiaoxin Sheng

3 Replies

    • Haydos's avatar
      Haydos
      Advocate I

      Thanks Rita, I've added some sample data to my original post now.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Haydos,

    I modify the formula to get each status based on current date fields and merge them to show as text.

    Record Status = 
    VAR SelectedDate =
        MAX ( DateTable[Date] )
    VAR CancelledDate =
        MAX ( T1[CancelledDate] )
    VAR SettlementDate =
        MAX ( T1[SettlementDate] )
    VAR ContractIssuedDate =
        MAX ( T1[ContractIssuedDate] )
    VAR SoldDate =
        MAX ( T1[SoldDate] )
    VAR isCancelled =
        IF (
            CancelledDate <= SelectedDate
                && NOT ISBLANK ( CancelledDate ),
            "Cancelled"
        )
    VAR isSettled =
        IF (
            SettlementDate <= SelectedDate
                && NOT ISBLANK ( SettlementDate ),
            "Settled"
        )
    VAR isContractIssued =
        IF (
            ContractIssuedDate <= SelectedDate
                && NOT ISBLANK ( ContractIssuedDate ),
            "Contract Issued"
        )
    VAR isSold =
        IF ( SoldDate <= SelectedDate && NOT ISBLANK ( SoldDate ), "Sold" )
    VAR merged =
        IF ( isCancelled <> BLANK (), isCancelled & ";" )
            & IF ( isSettled <> BLANK (), isSettled & ";" )
            & IF ( isContractIssued <> BLANK (), isContractIssued & ";" )
            & IF ( isSold <> BLANK (), isSold )
    RETURN
        IF ( merged <> BLANK (), merged, "N/A" )

    Then you can add a new table with all status types and use it as category of the table visual and write a measure formula to count correspond status from the concatenated text.

    Status Count = 
    VAR currStatus =
        SELECTEDVALUE ( NewTable[Status] )
    VAR summary =
        SUMMARIZE (
            ALLSELECTED ( T1 ),
            [Customer],
            [SoldDate],
            [ContractIssuedDate],
            [SettlementDate],
            [CancelledDate],
            "Status", [Record Status]
        )
    RETURN
        COUNTROWS (
            FILTER ( summary, SEARCH ( currStatus, [Record Status], 1, -1 ) > 0 )
        )


    Regards,

    Xiaoxin Sheng