Forum Discussion

fellin's avatar
fellin
Regular Visitor
5 years ago
Solved

Dynamically calculate Count of Rows for a specific Year/Month

Hi all, I'm pretty new to PowerBi and can't find anything which pushes me in the right direction. My problem: I have a table with diffrent Actions with a start and a end date, like this below: ...
  • v-alq-msft's avatar
    5 years ago

    Hi, fellin 

     

    Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

    Table:

     

    Calendar(a calcualted table):

    Calendar = 
    ADDCOLUMNS(
        CALENDARAUTO(),
        "YearMonth",
        YEAR([Date])*100+MONTH([Date])
    )

     

    You may create three measures as below.

    Count Start = 
    var ym = MAX('Calendar'[YearMonth])
    return
    COALESCE(
        COUNTROWS(
            FILTER(
                ALLSELECTED('Table'),
                (YEAR([Start Date])*100+MONTH([Start Date]))=ym
            )
        ),0
    )
    Count End = 
    var ym = MAX('Calendar'[YearMonth])
    return
    COALESCE(
        COUNTROWS(
            FILTER(
                ALLSELECTED('Table'),
                (YEAR([End Date])*100+MONTH([End Date]))=ym
            )
        ),0
    )
    Count Open = 
    var x = MAX('Calendar'[YearMonth])
    var tab = 
    ADDCOLUMNS(
        ALLSELECTED('Table'),
        "Flag",
        var t = 
        SELECTCOLUMNS( 
            ADDCOLUMNS(
                CALENDAR(
                    [Start Date],
                    [End Date]
                ),
                "ym",
                YEAR([Date])*100+MONTH([Date])
            ),
            "ym",[ym]
        )
        return 
        IF(
           x in t,
           1,0
        )
    )
    return
    COALESCE(
        COUNTROWS(
            FILTER(
                tab,
                [Flag]=1
            )
        ),
        0
    )

     

    Result:

     

    Best Regards

    Allan

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.