Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

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.

Reply
Anonymous
Not applicable

Create duplicate rows based on criteria

Hi,

 

This is my first post and I am very new to Power BI. So any help is appreciated.

 

I want to duplicate rows of my data, based on certain criteria. My data looks like below (With few more columns). [DAX column] is a calculated column I have created with (if else condition). 

 

I want to duplicate rows where my period is current period (here for example current period is 5), for all the years in data. So for both 2021 and 2022. But with this, I also want to populate data in my [DAX column] for newly cretaed rows. But that data will not be same as main data.

So I want in total 3 rows for every month data:

Row 1- Where Period-5, Year -2021, [DAX column]: YTD LY

Row 2- Where Period-5, Year -2022 and Indicator- Actual,  [DAX column]: YTD Actual

Row -3 Wherev Period-5, Year -2022 and Indicator- Forecast,  [DAX column]: YTD Forecast

 

YearPeriodIndicatorDAX column
20211ActualYTD LY
20212ActualYTD LY
20213ActualYTD LY
20214ActualYTD LY
20215ActualMTD LY
20221ForecastYTD Forecast
20222ForecastYTD Forecast
20223ForecastYTD Forecast
20224ForecastYTD Forecast
20225ForecastMTD Forecast
20221ActualMTD Actual
20222ActualMTD Actual
20223ActualMTD Actual
20224ActualMTD Actual
20225ActualMTD Actual

 

Logic behind [DAX column]:

if
[Fiscal Period]=5 and [Fiscal Year]=2021 then "MTD LY"
else if
[Fiscal Period]=5 and [Fiscal Year]=2022 and [Indicator"]="Actual" then "MTD Actual"
else if
[Fiscal Period]=5 and [Fiscal Year]=2022 and [Indicator"]="Forecast" then "MTD Forecast"

else if
[Fiscal Period]<=5 and [Fiscal Year]=2021 then "YTD LY"

else if
[Fiscal Period]<=5 and [Fiscal Year]=2022 and [Indicator"]="Actual" then "YTD Actual"
else if
[Fiscal Period]<=5 and [Fiscal Year]=2022 and [ndicator"]="Forecast" then "YTD Forecast"
else null

 

Above logic is not wotking for YTD values, as i only have 1 row for current period and it is getting tagged for MTD. But current period will also be part of YTD calculation.

 

 

Desired Output

 

YearPeriodIndicatorDAX columnDesired column
20211ActualYTD LYYTD LY
20212ActualYTD LYYTD LY
20213ActualYTD LYYTD LY
20214ActualYTD LYYTD LY
20215ActualMTD LYMTD LY
20215Actual YTD LY
20221ForecastYTD ForecastYTD Forecast
20222ForecastYTD ForecastYTD Forecast
20223ForecastYTD ForecastYTD Forecast
20224ForecastYTD ForecastYTD Forecast
20225ForecastMTD ForecastMTD Forecast
20225Forecast YTD Forecast
20221ActualMTD ActualMTD Actual
20222ActualMTD ActualMTD Actual
20223ActualMTD ActualMTD Actual
20224ActualMTD ActualMTD Actual
20225ActualMTD ActualMTD Actual
20225Actual YTD Actual

 

Thanks,

Shradha

1 ACCEPTED SOLUTION
tex628
Community Champion
Community Champion

Hi @Anonymous, 

I would propose something along the following. 

Start by making a reference to your calendar table in Power Query so you have a copy of the original table to work with. 

Follow this up by using the DateTime.LocalNow (DateTime.LocalNow - PowerQuery M | Microsoft Docs) to identify the current period and Year. 

Now we know that we want to remove everything apart from the following:

"Period" - "Last year" - "Actual"
"Period" - "Current year" - "Actual" 
"Period" - "Current year" - "Forecast"

So with the help of LocalNow you should be able to create a filter statement to remove the other rows. 

The final step would be to create the "Desired Values" column, which you should be able to do by looking at the Year and Indicator columns. 

Append the table back into the original table, and remove the load on the temporary one. 

Let me know how it goes! 

Br, 
Johannes


Connect on LinkedIn

View solution in original post

2 REPLIES 2
tex628
Community Champion
Community Champion

Hi @Anonymous, 

I would propose something along the following. 

Start by making a reference to your calendar table in Power Query so you have a copy of the original table to work with. 

Follow this up by using the DateTime.LocalNow (DateTime.LocalNow - PowerQuery M | Microsoft Docs) to identify the current period and Year. 

Now we know that we want to remove everything apart from the following:

"Period" - "Last year" - "Actual"
"Period" - "Current year" - "Actual" 
"Period" - "Current year" - "Forecast"

So with the help of LocalNow you should be able to create a filter statement to remove the other rows. 

The final step would be to create the "Desired Values" column, which you should be able to do by looking at the Year and Indicator columns. 

Append the table back into the original table, and remove the load on the temporary one. 

Let me know how it goes! 

Br, 
Johannes


Connect on LinkedIn
Anonymous
Not applicable

Thanks @tex628 

 

I cretaed reference of my original table and applied filters on fileds. Then adjusted my DAX Column logic to adjust YTD values. Also created one more filed and tagged all values in this refrenced table as duplicate, so that I can use this as filter when I will have to show KPIs in viz. Then appended both tables and disabled load for previous both tables.

Thank you for your support 🙂

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.