DAX技术咨询:统计general_report表上月创建记录数的实现问题
Hey there! Let's break down why your custom column attempt didn't work and get you a working solution, plus fix up that existing measure while we're at it.
First: Spot the Issue in Your Original Measure
Your current measure uses TODAY() - MONTH(TODAY()) to calculate "one month ago"—but this is a mistake! MONTH(TODAY()) returns a number between 1-12 (like 10 for October), so you're subtracting N days instead of rolling back a full calendar month. For example, if today is October 15, you'd be subtracting 10 days to get October 5, not September 15.
The correct way to get the date exactly one month prior is using the EDATE function:
_created_last_month = COUNTROWS( FILTER( general_report, [Created] >= EDATE(TODAY(), -1) ) )
Why Your Custom Column Failed
Custom columns calculate a value for every row in your table, whereas measures calculate values dynamically based on filters. If you tried to put COUNTROWS directly in a custom column, it would return the total number of rows in the filtered table for every single row—not a row-specific flag to count later.
Step-by-Step Custom Column Solution
Here's how to create a working custom column setup to count last month's entries:
Validate Your [Created] Column Type
First, make sure your[Created]column is set to a Date/Time data type. If it's stored as text, convert it first with this formula (adjust the format string if needed):Created_DateTime = PARSEDATE([Created], "dd/mm/yyyy hh:mm:ss")Create a Row-Level Flag Column
Add a new custom column to mark whether each row was created in the last month:Is_Last_Month = VAR One_Month_Ago = EDATE(TODAY(), -1) RETURN IF([Created_DateTime] >= One_Month_Ago, 1, 0) // Use [Created] if it's already a Date/Time typeCalculate the Total Count
Now you can sum this flag column to get the total number of entries from the last month. You can do this with a measure (best for dynamic updates):Total_Last_Month_Entries = SUM(general_report[Is_Last_Month])
Key Notes
- Dynamic vs Static: Measures are dynamic—they'll update automatically when today's date changes. The custom flag column also updates with the date, so the sum will reflect the current timeframe whenever you refresh your data.
- Edge Cases:
EDATEhandles month-end correctly (e.g., if today is October 31,EDATE(TODAY(), -1)returns September 30, since September has no 31st).
内容的提问来源于stack exchange,提问作者TheSeller

