Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

If command

Hello, I want to define no. of weeks for two cases in the table 'Geschäftskunden', but I get error:   Anzahl Woche = VAR _StartDate = SELECTEDVALUE('Geschäftskunden'[Startdatum]) VAR _EndDate = ...
  • bhanu_gautam's avatar
    1 year ago

    Anonymous , Try using

    DAX
    Anzahl Woche =
    VAR _StartDate = SELECTEDVALUE('Geschäftskunden'[Startdatum])
    VAR _EndDate = SELECTEDVALUE('Geschäftskunden'[Enddatum])
    RETURN
    IF (
    ISBLANK(_EndDate),
    DATEDIFF(_StartDate, TODAY(), WEEK),
    DATEDIFF(_StartDate, _EndDate, WEEK)
    )

  • v-ssriganesh's avatar
    1 year ago

    Hello Anonymous,
    Thank you for reaching out to the Microsoft Fabric Forum Community.

    I have reproduced your scenario in Power BI Desktop using sample data, and I can confirm that the calculation is working as expected.

    Summary of what I did:

    • Created a sample table Geschäftskunden with both populated and blank Enddatum values.
    • Used the following DAX formula in a calculated column to handle both cases correctly:
    Anzahl Woche =
    
    VAR _StartDate = 'Geschäftskunden'[Startdatum]
    
    VAR _EndDate = 'Geschäftskunden'[Enddatum]
    
    RETURN
    
    IF(
    
        ISBLANK(_EndDate),
    
        DATEDIFF(_StartDate, TODAY(), WEEK),
    
        DATEDIFF(_StartDate, _EndDate, WEEK)
    
    )

    Result:
    The column Anzahl Woche now shows the expected number of weeks:

    • When Enddatum is blank, it uses Today().
    • When Enddatum is present, it uses Enddatum.

    I am attaching the sample .pbix file here for your reference so you can review and adapt it to your own dataset.

    Thank you, bhanu_gautam & Ritaf1983 for sharing valuable insights.

     

    If this information is helpful, please “Accept as solution” and give a "kudos" to assist other community members in resolving similar issues more efficiently.
    Thank you.