Creating Summary Measures in Power BI for Date Columns: Counting Rows by Year

One common requirement when working with data is summarizing information based on date columns. For example, you might need to count how many rows were created in each year to understand data trends over time. This article will guide you through creating a dynamic summary in Power BI that counts rows based on dates, effectively producing a histogram by year.

In addition to the basic setup, we’ll provide 30 examples demonstrating how to use DAX to create various time-based summaries.

Step-by-Step: Creating a Measure to Count Rows by Year

Step 1: Create a Calculated Column for Year

To begin, you need to extract the year from the date column, which allows you to group data by year.

Here’s how to do it:

  1. Navigate to your table with the Date column.
  2. Create a new calculated column using the YEAR function to extract the year from each date.
YearColumn = YEAR('YourTable'[Date])

This column will hold the year for each entry in your dataset.

Step 2: Create a Measure to Count Rows by Year

Once you have the Year column, you can create a measure to count the number of rows for each year.

  1. Create a new measure that counts the number of rows using the COUNTROWS function.
RowCountByYear = COUNTROWS('YourTable')

Step 3: Build a Histogram

Next, you’ll create a visualization to display this data as a histogram:

  1. Drag the YearColumn into the Axis field of a bar chart or column chart.
  2. Add the RowCountByYear measure to the Values field.

This will produce a chart showing how many rows were created for each year, similar to a histogram.

Step 4: Dynamic Year-Based Row Count Measure

If you prefer not to create an extra column for the year and want to calculate it dynamically within a measure, you can use the following DAX code:

RowCountDynamicYear = 
CALCULATE(
    COUNTROWS('YourTable'),
    YEAR('YourTable'[Date])
)

This measure dynamically calculates the row count based on the year extracted from the Date column without needing an extra column.


30 Examples of Time-Based Summaries with DAX

Now that we have the foundation, let’s dive into 30 examples that illustrate different ways you can summarize and count rows based on dates using DAX.

Basic Counting Examples

  1. Count Rows by Year
RowCountByYear = COUNTROWS('YourTable')
  1. Count Rows by Month
RowCountByMonth = COUNTROWS('YourTable')
  1. Count Rows for Specific Year (e.g., 2020)
RowCount2020 = 
CALCULATE(
    COUNTROWS('YourTable'),
    YEAR('YourTable'[Date]) = 2020
)
  1. Count Rows for Dates After 2015
RowCountAfter2015 = 
CALCULATE(
    COUNTROWS('YourTable'),
    'YourTable'[Date] > DATE(2015, 12, 31)
)
  1. Count Rows Between Two Dates
RowCount2010To2020 = 
CALCULATE(
    COUNTROWS('YourTable'),
    'YourTable'[Date] >= DATE(2010, 1, 1),
    'YourTable'[Date] <= DATE(2020, 12, 31)
)

Cumulative and Running Totals

  1. Cumulative Count by Year
CumulativeCount = 
CALCULATE(
    COUNTROWS('YourTable'),
    FILTER(
        ALL('YourTable'[Date]),
        'YourTable'[Date] <= MAX('YourTable'[Date])
    )
)
  1. Running Total by Month
RunningTotalMonth = 
CALCULATE(
    COUNTROWS('YourTable'),
    FILTER(
        ALL('YourTable'[Date]),
        'YourTable'[Date] <= MAX('YourTable'[Date])
    )
)
  1. Cumulative Count for a Specific Year
CumulativeCount2020 = 
CALCULATE(
    COUNTROWS('YourTable'),
    'YourTable'[Date] <= DATE(2020, 12, 31)
)
  1. Cumulative Count of Rows from the Beginning
CumulativeFromStart = 
CALCULATE(
    COUNTROWS('YourTable'),
    FILTER(
        ALL('YourTable'[Date]),
        'YourTable'[Date] <= MAX('YourTable'[Date])
    )
)

Advanced Filtering Examples

  1. Count Rows by Quarter
RowCountByQuarter = 
CALCULATE(
    COUNTROWS('YourTable'),
    QUARTER('YourTable'[Date])
)
  1. Count Rows by Day of the Week
RowCountByDayOfWeek = 
CALCULATE(
    COUNTROWS('YourTable'),
    WEEKDAY('YourTable'[Date])
)
  1. Count Rows for a Range of Years
RowCount2015To2020 = 
CALCULATE(
    COUNTROWS('YourTable'),
    YEAR('YourTable'[Date]) >= 2015,
    YEAR('YourTable'[Date]) <= 2020
)
  1. Count Rows for Current Year
RowCountCurrentYear = 
CALCULATE(
    COUNTROWS('YourTable'),
    YEAR('YourTable'[Date]) = YEAR(TODAY())
)
  1. Count Rows for the Last 5 Years
RowCountLast5Years = 
CALCULATE(
    COUNTROWS('YourTable'),
    YEAR('YourTable'[Date]) >= YEAR(TODAY()) - 5
)

Time Intelligence Functions

  1. Count Rows Year-to-Date
YTDRowCount = 
CALCULATE(
    COUNTROWS('YourTable'),
    DATESYTD('YourTable'[Date])
)
  1. Count Rows Month-to-Date
MTDRowCount = 
CALCULATE(
    COUNTROWS('YourTable'),
    DATESMTD('YourTable'[Date])
)
  1. Count Rows Last Year
LastYearRowCount = 
CALCULATE(
    COUNTROWS('YourTable'),
    SAMEPERIODLASTYEAR('YourTable'[Date])
)
  1. Count Rows Last Month
LastMonthRowCount = 
CALCULATE(
    COUNTROWS('YourTable'),
    PREVIOUSMONTH('YourTable'[Date])
)
  1. Count Rows for the Previous Quarter
PreviousQuarterRowCount = 
CALCULATE(
    COUNTROWS('YourTable'),
    PREVIOUSQUARTER('YourTable'[Date])
)

Time Period Filtering

  1. Count Rows by Fiscal Year
FiscalYearRowCount = 
CALCULATE(
    COUNTROWS('YourTable'),
    YEAR('YourTable'[FiscalDate])
)
  1. Count Rows by Custom Period
CustomPeriodRowCount = 
CALCULATE(
    COUNTROWS('YourTable'),
    'YourTable'[Date] >= DATE(2021,1,1),
    'YourTable'[Date] <= DATE(2021,3,31)
)
  1. Count Rows for Current Month
RowCountCurrentMonth = 
CALCULATE(
    COUNTROWS('YourTable'),
    MONTH('YourTable'[Date]) = MONTH(TODAY())
)
  1. Count Rows for Current Quarter
RowCountCurrentQuarter = 
CALCULATE(
    COUNTROWS('YourTable'),
    QUARTER('YourTable'[Date]) = QUARTER(TODAY())
)
  1. Count Rows for Year-End
YearEndRowCount = 
CALCULATE(
    COUNTROWS('YourTable'),
    ENDOFYEAR('YourTable'[Date])
)

Conclusion

These 30 examples provide a broad overview of how to perform time-based counting and filtering using DAX in Power BI. From counting rows per year to advanced time intelligence, these techniques help summarize and analyze your data based on dates.

For further reference and a complete list of DAX functions, check out the official Microsoft DAX documentation.

Edvaldo Guimrães Filho Avatar

Published by

Categories:

Leave a comment