Don't miss your chance to take the Fabric Data Engineer (DP-700) exam on us!
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
Let's say I have this table below
| Event | Year | Revenue |
Event A | 2020 | 10000 |
| Event A | 2021 | 8000 |
| Event A | 2022 | 11000 |
| Event A | 2024 | 5000 |
Notice that there is no revenue for 2023. I want to create a new calculated column called "Prior Year". The logic for the prior year column would be based on whether that prior year sales is blank or not. If the prior year sales is not blank then return that as the prior year, otherwise if it is blank then go back another year until a non blank year is found. So in the scenario above, the year 2024 shoud have 2022 as its prior year due to the fact that 2023 has no revenue. So my output table would look like below.
| Event | Year | Revenue | Prior Year |
| Event A | 2020 | 10000 | blank |
| Event A | 2021 | 8000 | 2020 |
| Event A | 2022 | 11000 | 2021 |
| Event A | 2024 | 5000 | 2022 |
How do I achieve this?
Solved! Go to Solution.
@RingoMoon , Try using below method create a new calculated column, replace column and table name
Prior Year =
VAR CurrentYear = [EventYearRevenue]
VAR PriorYearRevenue =
CALCULATE(
MAX([EventYearRevenue]),
FILTER(
ALL('TableName'),
[EventYearRevenue] <> BLANK() && [EventYearRevenue] < CurrentYear
)
)
RETURN
IF(ISBLANK(PriorYearRevenue), BLANK(), PriorYearRevenue)
Proud to be a Super User! |
|
This should do the trick
Prior Year =
VAR _CurrentYear = SolutionAlex[Year]
VAR _PriorYear = _CurrentYear - 1
VAR _FindYear =
CALCULATE(
MAX(SolutionAlex[Year]),
SolutionAlex[Year] < _CurrentYear,
NOT(ISBLANK(SolutionAlex[Revenue]))
)
VAR _Value = CALCULATE(SUM(SolutionAlex[Revenue]), ALL(SolutionAlex), SolutionAlex[Year] = _FindYear)
VAR _Result =
IF(
ISBLANK(_Value),
BLANK(),
_Value
)
RETURN
_FindYear
If you want the Prior Year, then return _FindYear as in my DAX, if you want the associated revenues, return _Result.
If it answers your query, please mark my reply as the solution. Thanks!
Don't forget to change the table name 'SolutionAlex' with your tableName
Hi @RingoMoon ,
You can do it in Power Query or using dax.
Power Query:
try
if #"Added Index"{[Index]-1}[Event] = [Event]
then
#"Added Index"{[Index]-1}[Revenue] else null
otherwise
null
DAX
Add the following column:
VAR temp_table = SUMMARIZECOLUMNS(
'Table'[Event],
'Table'[Year],
'Table'[Revenue]
)
RETURN
SELECTCOLUMNS(
OFFSET(
-1,
temp_table,
ORDERBY('Table'[Year]),
,
PARTITIONBY('Table'[Event])
),
'Table'[Revenue]
)
Regards
Miguel Félix
Proud to be a Super User!
Check out my blog: Power BI em PortuguêsHi @RingoMoon ,
You can do it in Power Query or using dax.
Power Query:
try
if #"Added Index"{[Index]-1}[Event] = [Event]
then
#"Added Index"{[Index]-1}[Revenue] else null
otherwise
null
DAX
Add the following column:
VAR temp_table = SUMMARIZECOLUMNS(
'Table'[Event],
'Table'[Year],
'Table'[Revenue]
)
RETURN
SELECTCOLUMNS(
OFFSET(
-1,
temp_table,
ORDERBY('Table'[Year]),
,
PARTITIONBY('Table'[Event])
),
'Table'[Revenue]
)
Regards
Miguel Félix
Proud to be a Super User!
Check out my blog: Power BI em PortuguêsThis should do the trick
Prior Year =
VAR _CurrentYear = SolutionAlex[Year]
VAR _PriorYear = _CurrentYear - 1
VAR _FindYear =
CALCULATE(
MAX(SolutionAlex[Year]),
SolutionAlex[Year] < _CurrentYear,
NOT(ISBLANK(SolutionAlex[Revenue]))
)
VAR _Value = CALCULATE(SUM(SolutionAlex[Revenue]), ALL(SolutionAlex), SolutionAlex[Year] = _FindYear)
VAR _Result =
IF(
ISBLANK(_Value),
BLANK(),
_Value
)
RETURN
_FindYear
If you want the Prior Year, then return _FindYear as in my DAX, if you want the associated revenues, return _Result.
If it answers your query, please mark my reply as the solution. Thanks!
Don't forget to change the table name 'SolutionAlex' with your tableName
@RingoMoon , Try using below method create a new calculated column, replace column and table name
Prior Year =
VAR CurrentYear = [EventYearRevenue]
VAR PriorYearRevenue =
CALCULATE(
MAX([EventYearRevenue]),
FILTER(
ALL('TableName'),
[EventYearRevenue] <> BLANK() && [EventYearRevenue] < CurrentYear
)
)
RETURN
IF(ISBLANK(PriorYearRevenue), BLANK(), PriorYearRevenue)
Proud to be a Super User! |
|
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 53 | |
| 45 | |
| 44 | |
| 20 | |
| 19 |
| User | Count |
|---|---|
| 73 | |
| 71 | |
| 34 | |
| 33 | |
| 31 |