Search code examples
powerbidaxpowerbi-desktoppowerbi-embedded

Compare Selected quarter to last quarter in DAX, even when i select Q1 it should compare with last year's Q4


Hi i am working on a solution in DAX where i have to find customers not ordered in current Quarter compared to its last quarter, i am able to compare Q2, Q3 and Q4 but when i select Q1 i get no value. if i select it should compare with Q4 of last year how this can be achieved? Thanks

this was my original question get new customers compared to last month in dax power bi

with a little bit of tinkering i am here now

Customers Not ordered This Quarter = 
VAR ThisQuarter =
    SELECTEDVALUE( DailyReport[DateCreated].[QuarterNo])
VAR ThisYEAR =
    SELECTEDVALUE(DailyReport[DateCreated].[Year])
VAR SelectedSupplier = 
    SELECTEDVALUE(DailyReport[SupplierName])
VAR LastQuarter = ThisQuarter - 1
VAR CustomersThisQuarter =
    DISTINCT(
        SELECTCOLUMNS(
            FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = ThisQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = ThisYEAR),
            "C1", DailyReport[VenueName]
        )
    )
VAR CustomersLastQuarter =
    DISTINCT(
        SELECTCOLUMNS(
            FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = LastQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = ThisYEAR),
            "C1", DailyReport[VenueName]
        )
    )
VAR T1 =
    EXCEPT(CustomersLastQuarter, CustomersThisQuarter )
RETURN
    CONCATENATEX( T1, [C1], UNICHAR(10), [C1], ASC)

it should compare with the last year's Q4 when i select Q1 in current year


Solution

  • You really need a date table. I have written the code below blind but it should work.

       Customers Not ordered This Quarter = 
        VAR ThisQuarter =
            SELECTEDVALUE( DailyReport[DateCreated].[QuarterNo])
        VAR ThisYEAR =
            SELECTEDVALUE(DailyReport[DateCreated].[Year])
        VAR SelectedSupplier = 
            SELECTEDVALUE(DailyReport[SupplierName])
        VAR LastQuarter = IF(ThisQuarter = 1,4,ThisQuarter - 1)
        VAR LastYear = IF(ThisQuarter = 1,ThisYEAR -1,ThisYEAR)
        
        VAR CustomersThisQuarter =
            DISTINCT(
                SELECTCOLUMNS(
                    FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = ThisQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = ThisYEAR),
                    "C1", DailyReport[VenueName]
                )
            )
        VAR CustomersLastQuarter =
            DISTINCT(
                SELECTCOLUMNS(
                    FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = LastQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = LastYEAR),
                    "C1", DailyReport[VenueName]
                )
            )
        VAR T1 =
            EXCEPT(CustomersLastQuarter, CustomersThisQuarter )
        RETURN
            CONCATENATEX( T1, [C1], UNICHAR(10), [C1], ASC)