To determine the starting day of a month given a column with the Year and another column with the Month using DAX (Data Analysis Expressions) in Power BI, Power Pivot, or similar Microsoft BI tools, you can use the DATE function. The DATE function creates a date for a specified year, month, and day. Since you want the starting day of the month, you'll always use 1 as the day parameter. Here's how you can do it:
DAX Formula
Assuming your table is named Table and your columns are named Year and Month, you can add a new calculated column using the following DAX formula:
dax
Start of Month = DATE('Table'[Year], 'Table'[Month], 1)
Explanation:
'Table'[Year]: Refers to the value in the Year column of the Table for each row.
'Table'[Month]: Refers to the value in the Month column of the Table for each row. Note that DAX expects the month as a number (1 for January, 2 for February, ..., 12 for December).
1: Since you want the first day of the month, 1 is used as the day parameter.
Applying the Formula:
In Power BI or Power Pivot:
Go to the Modeling tab.
Click on New Column.
In the formula bar, type or paste the DAX formula provided above.
Press Enter. A new column named "Start of Month" will be added to your table with the first day of the month for each row based on the Year and Month columns.
If You Prefer a Measure Instead of a Column (Less Common for This Use Case):
Measures are more suited for calculations that you want to use in the context of a report (e.g., in a table or chart that's filtered by other elements). Since determining the start of a month is typically a per-row calculation, a calculated column is more appropriate. However, if you have a specific need for a measure, the formula would conceptually be the same, but you'd use the SELECTEDVALUE function to contextually determine the year and month from your filters or selections.
Example Measure (Not Recommended for This Scenario):
dax
Start of Selected Month =
VAR SelectedYear = SELECTEDVALUE('Table'[Year])
VAR SelectedMonth = SELECTEDVALUE('Table'[Month])
RETURN
IF(
NOT ISBLANK(SelectedYear) && NOT ISBLANK(SelectedMonth),
DATE(SelectedYear, SelectedMonth, 1),
BLANK()
)
Use Case for Measure:
A measure would be useful if you want to dynamically show the start of the month based on user selections in a report. For example, if you have slicers for Year and Month, this measure can return the first day of the selected month and year, updating automatically as the user changes their selection.
Note: This measure example is for educational purposes, showing how to handle dynamic selections. For the specific task of finding the start of the month for each row in a table, the calculated column approach is more straightforward and efficient.
Practical Example:
Suppose you have a table like this:
Year
Month
2024
1
2024
2
2024
3
After adding the calculated column, your table will look like this: