Forum Discussion

Shota_Xuc's avatar
Shota_Xuc
Helper I
2 years ago

Count rows with same value in one column

Hello,

 

I have a column "Valuation date" with dates like:

2024-02-06

2024-02-06

2024-02-06

2024-02-07

2024-02-07

2024-02-07

2024-02-09

2024-02-10

2024-02-10

2024-02-12

2024-02-12

2024-02-12

2024-02-12

I want to create a measure showing how many times i have this date in the column like:

2024-02-06     3

2024-02-06     3

2024-02-06     3

2024-02-07     3

2024-02-07     3

2024-02-07     3

2024-02-09     1

2024-02-10     2

2024-02-10     2

2024-02-12     4

2024-02-12     4

2024-02-12     4

2024-02-12     4

 

Please help with DAX. Thanks!

11 Replies

  • Shota_Xuc , You can try below mentioned measure

     

    Count of Valuation Dates =
    COUNTROWS(
    FILTER(
    'YourTableName',
    'YourTableName'[Valuation date] = EARLIER('YourTableName'[Valuation date])
    )
    )

     

    Please accept as solution and give kudos if it helps

    • gmsamborn's avatar
      gmsamborn
      Super User

      Hi bhanu_gautam 

       

      That DAX won't work for a measure because it "refers to an earlier row context which doesn't exist."

       

      It would work as a calculated column.

  • bhanu_gautam gmsamborn 

     

    yes, exactly, that does not work as a measure because it "refers to an earlier row context which doesn't exist." But I do not need to have it as a calculcated column, if I have it as a column, I get numbers from the ground table (which has more rows than my visual) and not from my visual.

      • Shota_Xuc's avatar
        Shota_Xuc
        Helper I

        Hi gmsamborn MAX does not work either, as you see I get 2 instead of 3 for 2024-02-06, 4 instead of 1 for 2024-02-09, 6 instead of 2 for 2024-02-13 and etc.

         

  • NickDanger's avatar
    NickDanger
    Regular Visitor

    I'm assuming it's intended that you want the dates to display multiple times as well in the visual. This would typically mean either
    a) you're not using a date table or
    b) there are going to be more columns added to the display table and a date table is going to be used.

    Either way, the following calculation should do the job for you. 'Table' in this case is going to be the table that your fact is (I.E. not the date table)

    CountOfDates = 
    var _SelectedValueDate = SELECTEDVALUE('Table'[Date])
    return
    CALCULATE(
         COUNTROWS('Table'),
         ALL('Table'),
         'Table'[Date] = _SelectedValueDate
    )



    • Shota_Xuc's avatar
      Shota_Xuc
      Helper I

      Hi NickDanger, it does not work. E.g. I should get 3 for 02-06, 1 for 02-09, 2 for 02-13 instead.

      I think the expression takes numbers from ground table my visual is based on, but rows in my visual are filtered out. I need to count rows with the same valuation date on my visual below.

       

      • NickDanger's avatar
        NickDanger
        Regular Visitor

        Shota_Xuc 
        Thanks for the elaboration. The original post did have much info. Here is a code snippest of test data from AdventureWorks that I mocked up that would work with your issue.

        Test Measure = 
        var _SelectedValueDate = SELECTEDVALUE('FactInternetSales'[OrderDate])
        RETURN
        CALCULATE(
            COUNTROWS('FactInternetSales'),
            ALLEXCEPT('FactInternetSales', 'FactInternetSales'[PromotionKey]), //Add any columns here that you're filtering on
            'FactInternetSales'[OrderDate] = _SelectedValueDate)

        Include any of the columns you're filtering your table on in the ALLEXCEPT function. For instance, in my data I filtered on PromotionKey. 
        This first screenshot is without including PromotionKey in the ALLEXCEPT, the second screenshot is when it's written to account for the filter.