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
bmacman
Frequent Visitor

Create new column based on value from another column with a specific date range and matching ID

Hi all,

Trying to create a new column in a table. This column needs get data from another table with a time +/- 30 seconds from Table1.

Table1 already has a minute for every minute of the day column

 

In the end I am looking to fill table1 with the IF table2 mode2 has a value at the same time as Table1 then use Table2 data, if not Table2 vlaue exists use Table1 data.

bmacman_0-1672181756186.png

I was thinking the IF statement would work for this but it seems to not recognise the column in Table2.
I cant work in the M query zone as these 2 mode columns are based on IF states already.
I tried the below and it did not work.
NewColumn1 =
IF
('Table2'[minutesAddedToStartDate]>='Table1[minuteAddedToStartDate]+#duration(0,0,0,30)
&&
'Table2'[minutesAddedToStartDate]<'Table1[minuteAddedToStartDate]-#duration(0,0,0,30),
        'Table2[mode2],'Table1[mode1]
        )
 
Any thoughts on what I am doing wrong or how to fix this?
5 REPLIES 5
v-cgao-msft
Community Support
Community Support

Hi @bmacman ,

 

Suppose we have:

Table1:

vcgaomsft_9-1672195111974.png

Table2:

vcgaomsft_10-1672195136896.png

Please try the follow steps:

1.Group and Add index column and Merge queries:

How to create group index with Power Query

= Table.TransformColumns(#"Grouped Rows",{{"Data",each Table.AddIndexColumn(_,"Index",1,1)}})

vcgaomsft_0-1672199436393.png

2. expand table2 columns:

 

3. add a custom column:

 

if [minutesAddedToStartDate]-[minutesAddedToStartDate.1]<=#duration(0,0,0,30)
or 
[minutesAddedToStartDate]-[minutesAddedToStartDate.1]>=-#duration(0,0,0,30)
then [mode2]
else [mode1]

 

vcgaomsft_5-1672193799828.png

if [minutesAddedToStartDate]-[minutesAddedToStartDate.1]<=#duration(0,0,0,30)
or 
[minutesAddedToStartDate]-[minutesAddedToStartDate.1]>=-#duration(0,0,0,30)
then [mode2]
else [mode1]

4. remove columns and the result:

vcgaomsft_1-1672199540571.png

 

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Thanks @v-cgao-msft  have started this process, moved the IF columns from DAX to M and can start to work this through. For some reason though I am getting an error on the merge

bmacman_0-1672269582093.png

Not sure what this value is, I have no columns in either query called training anything.

 

amitchandak
Super User
Super User

@bmacman , a new column

 

=

var _max = maxx(Filter(Table2, 'Table2'[minutesAddedToStartDate] >= 'Table1'[minuteAddedToStartDate]+ #duration(0,0,0,30) && 'Table2'[minutesAddedToStartDate] <'Table1'[minuteAddedToStartDate]-#duration(0,0,0,30) ),
'Table2'[mode2])

 

return

if(isblank(_max), 'Table1'[mode1],_max)

 


refer 4 ways (related, relatedtable, lookupvalue, sumx/minx/maxx with filter) to copy data from one table to another
https://www.youtube.com/watch?v=Wu1mWxR23jU
https://www.youtube.com/watch?v=czNHt7UXIe8

bmacman_0-1672199062613.png

Hi @amitchandak, Thanks for the response

Tried this, gives me this error. which is the # at the start of the #duration

bmacman_1-1672199124727.png

 

It actually worked first run then tried to change the name of the merge query table and this error appeared

 

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.