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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
ChromeMystic
Helper I
Helper I

Can I create a lookup Table for Values between two dates in Powerquery?

I have solved this with DAX at the Moment but I would love to do it in PowerQuery, so I can create all my tables inside Dataflows.

I got a table with our KPI Values as well as the Start and end date.

 

 

Here is a Mockup on how it works.

StartDateEndDateKPINameGreenYellowRed
01.01.202131.12.2021KPIValue112

3

01.01.202131.12.2099KKPIValue259

16

01.01.202231.12.2099KPIValue11.52.5

3.5

 

In PowerBi Desktop I created a new Table that Pulls the Dates from my existing Datetable (although the Function on start and end Date I can easily Replicate in PowerQuery) and has Custom Rows that run the following DAX Code:

 

 

value1G = 
var __kpi = "kpiValue1" // set the KPI for this column
var __date = [Date] 
var __result = 
MAXX(
    FILTER(
        sedKPI,
            __date>=sedKPI[ValidFrom] && __date<=sedKPI[validTo] && sedKPI[KPI] = __kpi), // Filter if current date is between valid from and until
sedKPI[green] // select column that contains the value
)
return __result

 

 

This results in a lookuptable, that shows the current KPI Value for each day of my data. These I then use in my conditional Formatting and other functions for KPI breeches. As the KPI values change from time to time (normally once per year but we had changes that were done during a calendaryear as well) I don't just want to set a global value per KPI, as this would show the wrong results for older Data.

 

I found solutions on how to select data from a table based on one date with the Merge function in PowerQuery but I wasn't able to build something that checks wether todays date is between the start and enddate of my KPI Table and looks up the respective fields. Preferably I would do this with a custom function that gets the KPI value to look for as variable and then does the rest by itself.

1 ACCEPTED SOLUTION
v-yanjiang-msft
Community Support
Community Support

Hi @ChromeMystic ,

According to your DAX formula, here's my PowerQuery solution.

Create a custom column.

= Table.SelectRows(sedKPI, 
    		(sedKPI) =>(sedKPI[ValidFrom] <= [Date]) and
       		(sedKPI[ValidTo]  >=  [Date]) and 
       		(sedKPI[KPI] = "KPIValue1")
        	)[Green]{0}

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
ChromeMystic
Helper I
Helper I

Thank you!! This worked as I need it. 

v-yanjiang-msft
Community Support
Community Support

Hi @ChromeMystic ,

According to your DAX formula, here's my PowerQuery solution.

Create a custom column.

= Table.SelectRows(sedKPI, 
    		(sedKPI) =>(sedKPI[ValidFrom] <= [Date]) and
       		(sedKPI[ValidTo]  >=  [Date]) and 
       		(sedKPI[KPI] = "KPIValue1")
        	)[Green]{0}

I attach my sample below for reference.

 

Best Regards,
Community Support Team _ kalyj

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.

Top Solution Authors
Top Kudoed Authors