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 column based on lign value and interval from another table

Hello everyone!

 

I have been trying to resolve this issue for some time now but I'm stuck.

 

I have two tables : 

--> One describing sales for different products on specific days

ProductDaySales
A150
A240
A220
A420
A530
B120
B330
B540
B650
B820
C150
C420
C410

 

--> And another one listing for each product the beginning of "seasons" (different products don't have the same season beginning):

 

Productstart_new_season
A0
A3
A5
A10
B0
B2
B4
C0
C4

 

What I need is to add a column on my first table that specify the day of beginning of season the sales have been made in : 

 

ProductDaySalesstart_new_season
A1500
A2400
A2200
A4203
A5305
B1200
B3302
B5404
B6504
B7204
C1500
C4204
C4104

 

Thanks !

1 ACCEPTED SOLUTION
VahidDM
Super User
Super User

Hi @Anonymous 

 

Try this code to add a new column with dax to the sale table:

 

Start_New_Sale = 
CALCULATE (
    MAX ( 'product'[start_new_season] ),
    FILTER (
        ALL ( 'product' ),
        'product'[Product] = EARLIER ( sales[Product] )
            && 'product'[start_new_season] <= EARLIER ( sales[Day] )
    )
)

 

output:

VahidDM_0-1651100584667.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

Thanks @VahidDM and @speedramps ! Your help is really appreciated. I'll go for your recommandation Vahid and I took note of your remark speedramps

VahidDM
Super User
Super User

Hi @Anonymous 

 

Try this code to add a new column with dax to the sale table:

 

Start_New_Sale = 
CALCULATE (
    MAX ( 'product'[start_new_season] ),
    FILTER (
        ALL ( 'product' ),
        'product'[Product] = EARLIER ( sales[Product] )
            && 'product'[start_new_season] <= EARLIER ( sales[Day] )
    )
)

 

output:

VahidDM_0-1651100584667.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

Good answer VahidDM  but may I mention a potential problem.

Product C has two sales on day 4 (20 + 10 = 30).

If the user use the default sums for sales by product by day then it will returns a Start_New_Sale sum of 4+4 =8 instead of 4 .

So the users should alway display the value as min or max to return 4 rather than use default sum.

 

 

speedramps_0-1651101546262.png

 

@speedramps

 

That's not a problem, you mentioned a question that has a clear answer!

that can happened for any columns with different values and different aggregation, and you need to make sure to use your data with correct aggregation!!

 

Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

 

speedramps
Super User
Super User

Hi jlwork



Click here to download an example solution 

Please consider this solution.

I am a Power BI volunteer. Please click the thumbs up for me going to the effort of trying to helping you. Then click solved if I fix your problem. One problem per ticket please. If you need to expand or change your problem then please raise a new ticket and click solved on this one so we get kudos. Many thanks. 

 

Start of new season =
VAR myproduct = SELECTEDVALUE(Sales[Product])
VAR myday = SELECTEDVALUE(Sales[Day])
RETURN
CALCULATE(
MAX(Seasons[start_new_season]),
FILTER(Seasons,
Seasons[Product] = myproduct
&& Seasons[start_new_season] <= myday)
)

 

Anonymous
Not applicable

Hi @speedramps ! Thank you for your answer ! Do you think there is a way to have the results as a column and not as a measure ?

Hi again jlwork

 

It will be very difficult to to get a column because a product can have multiple season start dates and multiple sale dates,
eg product A seasons = 3, 5 and 10 and sales dates = 1,2,3,4,5.
So for sales date 4 you have to scan all the seasons to get the answer 3.
That is easy in Dax but difficult in Power Query.

 

You didnt originally specify it must be column.

Please click the thumbs up for me going to the effort of trying to helping you and click solved for the DAX solution so I get kudos. Many thanks. 

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.