Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Is there a way we can do this in Power BI?
My goal is to have a formula that will always determine the 'Current Week' and the 'Previous Week', the rest will show null values.
Is that possible in Power BI?
Solved! Go to Solution.
Hi @Anonymous ,
You can create a calculated column as below:
Week =
VAR _maxdate =
CALCULATE ( MAX ( 'Data'[Last Update Date] ), ALL ( 'Data' ) )
VAR _secondmaxdate =
CALCULATE (
MAX ( 'Data'[Last Update Date] ),
FILTER ( 'Data', 'Data'[Last Update Date] < _maxdate )
)
RETURN
IF (
'Data'[Last Update Date] = _maxdate,
"Current Week",
IF ( 'Data'[Last Update Date] = _secondmaxdate, "Previous Week", BLANK () )
)
You can also create a measure as below with similar formula:
Measure =
VAR _maxdate =
CALCULATE ( MAX ( 'Data'[Last Update Date] ), ALLSELECTED( 'Data' ) )
VAR _secondmaxdate =
CALCULATE (
MAX ( 'Data'[Last Update Date] ),
FILTER ( ALLSELECTED('Data'), 'Data'[Last Update Date] < _maxdate )
)
RETURN
IF (
SELECTEDVALUE('Data'[Last Update Date]) = _maxdate,
"Current Week",
IF (SELECTEDVALUE('Data'[Last Update Date]) = _secondmaxdate, "Previous Week", BLANK () )
)
Best Regards
Hey @Anonymous ,
that is possible with a calculated column. You just have to compare the weeknumber of the column to the today's week number.
Try the following formula:
Week =
VAR vCurrentWeek = WEEKNUM( TODAY() )
RETURN
SWITCH(
WEEKNUM( myTable[Date] ),
vCurrentWeek, "Current Week",
vCurrentWeek - 1, "Previous Week",
BLANK()
)
Hi, i tried to use the formula you said but it's not giving me the expected output. Please see below:
The logic i need is to get the "current" if it's the latest date, then get the "previous" if it's less than the latest date then the rest will be null.
Hi @Anonymous ,
You can create a calculated column as below:
Week =
VAR _maxdate =
CALCULATE ( MAX ( 'Data'[Last Update Date] ), ALL ( 'Data' ) )
VAR _secondmaxdate =
CALCULATE (
MAX ( 'Data'[Last Update Date] ),
FILTER ( 'Data', 'Data'[Last Update Date] < _maxdate )
)
RETURN
IF (
'Data'[Last Update Date] = _maxdate,
"Current Week",
IF ( 'Data'[Last Update Date] = _secondmaxdate, "Previous Week", BLANK () )
)
You can also create a measure as below with similar formula:
Measure =
VAR _maxdate =
CALCULATE ( MAX ( 'Data'[Last Update Date] ), ALLSELECTED( 'Data' ) )
VAR _secondmaxdate =
CALCULATE (
MAX ( 'Data'[Last Update Date] ),
FILTER ( ALLSELECTED('Data'), 'Data'[Last Update Date] < _maxdate )
)
RETURN
IF (
SELECTEDVALUE('Data'[Last Update Date]) = _maxdate,
"Current Week",
IF (SELECTEDVALUE('Data'[Last Update Date]) = _secondmaxdate, "Previous Week", BLANK () )
)
Best Regards
Thank you!
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
109 | |
98 | |
80 | |
64 | |
57 |
User | Count |
---|---|
145 | |
111 | |
92 | |
84 | |
66 |