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

Calculated field based on customer history : PowerBI

Hello,

I am a PowerBI new user. I am trying to see how I can create new calculated fields within PowerBI to be able afterwards to use predictive models.

Indicative example:

* To produce predictive variables, I need to enrich each row of the db ("shopping actions" table) with a new field. Each row represents a shopping action from a customer abc (customer id, amount paid, venue date,...). For row "x", I need to have a field (column to add) describing "past buying behaviour from this customer" corresponding at the "x" date in this row (venue date).

I need to go through the whole "shopping actions" table focusing only on rows where customer id = "abc" AND venue date < venue date of row "x".

How can PowerBI DAX functions help me in this please? This calculation should be done for every rows.

 

Thank you for your help.

 

Mohamed.

1 ACCEPTED SOLUTION

Hi @mohamedelham,

 

I think '#days since last Venue','Last Payment status' fields are you wanted, right? If this is a case, please try to use following measures:

 

History days = 
VAR last_date =
    CALCULATE (
        MAX ( 'Sample'[Venue date] ),
        FILTER ( ALLSELECTED ( 'Sample' ), [Venue date] < MAX ( [Venue date] ) ),
        VALUES ( 'Sample'[customerid] )
    )
RETURN
    IF (
        last_date = BLANK (),
        0,
        COUNTROWS ( CALENDAR ( last_date, MAX ( [Venue date] ) ) ) - 1
    )

Lsat Status = 
VAR last_date =
    CALCULATE (
        MAX ( 'Sample'[Venue date] ),
        FILTER ( ALLSELECTED ( 'Sample' ), [Venue date] < MAX ( [Venue date] ) ),
        VALUES ( 'Sample'[customerid] )
    )
RETURN
    IF (
        last_date <> BLANK (),
        LOOKUPVALUE (
            'Sample'[Payment status],
            Sample[customerid], SELECTEDVALUE ( 'Sample'[customerid] ),
            Sample[Venue date], last_date
        ),
        SELECTEDVALUE ( 'Sample'[Payment status] )
    )

6.PNG

 

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

8 REPLIES 8
mohamedelham
Frequent Visitor

Hello,

I am a PowerBI new user. I am trying to see how I can create new calculated fields within PowerBI to be able afterwards to use predictive models.

Indicative example:

* To produce predictive variables, I need to enrich each row of the db ("shopping actions" table) with a new field. Each row represents a shopping action from a customer abc (customer id, amount paid, venue date,...). For row "x", I need to have a field (column to add) describing "past buying behaviour from this customer" corresponding at the "x" date in this row (venue date).

I need to go through the whole "shopping actions" table focusing only on rows where customer id = "abc" AND venue date < venue date of row "x".

How can PowerBI DAX functions help me in this please? This calculation should be done for every rows.

 

Thank you for your help.

 

Kind Regards,

 

Mohamed.

v-shex-msft
Community Support
Community Support

Hi @mohamedelham,


Please share some sample data with table format with expected result, it will be help for testing and coding formula.

BTW, dax formula not contain any cache data features, if you means to use dax to record history operations, it is impossible.

 

Regards,

Xiaxoin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hello,

Thank you for your reply. Below and example:

* the fields 1 to 5 are given from client

* the fields from 6 and 7 should be calculated and based on cutomers history

** example: customer 321 came 3 times

*****the 1st stime he was new and then no field to calculate as no history

*****the 2nd time

************value "28" is calculated from difference between current venue date and last venue date //

************value "Paid" is calculated from payment statuts in his last shoppingAction column.

 

Thx for hep 🙂

 

ShoppingAction idcustomeridCategoryVenue datePayment status#days since last VenueLast Payment status
35158497855food29/09/2016Paid17Paid
54659217321food04/01/2017Unpaidunknownunknow
21548956321non food01/02/2017Unpaid28Unpaid
32658754215food02/03/2017Paid1Paid
54659887321non food01/05/2017Paid89Unpaid
15847989145non food27/09/2017Paid2Paid

Hi @mohamedelham,

 

I think '#days since last Venue','Last Payment status' fields are you wanted, right? If this is a case, please try to use following measures:

 

History days = 
VAR last_date =
    CALCULATE (
        MAX ( 'Sample'[Venue date] ),
        FILTER ( ALLSELECTED ( 'Sample' ), [Venue date] < MAX ( [Venue date] ) ),
        VALUES ( 'Sample'[customerid] )
    )
RETURN
    IF (
        last_date = BLANK (),
        0,
        COUNTROWS ( CALENDAR ( last_date, MAX ( [Venue date] ) ) ) - 1
    )

Lsat Status = 
VAR last_date =
    CALCULATE (
        MAX ( 'Sample'[Venue date] ),
        FILTER ( ALLSELECTED ( 'Sample' ), [Venue date] < MAX ( [Venue date] ) ),
        VALUES ( 'Sample'[customerid] )
    )
RETURN
    IF (
        last_date <> BLANK (),
        LOOKUPVALUE (
            'Sample'[Payment status],
            Sample[customerid], SELECTEDVALUE ( 'Sample'[customerid] ),
            Sample[Venue date], last_date
        ),
        SELECTEDVALUE ( 'Sample'[Payment status] )
    )

6.PNG

 

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Dear  Xiaoxin,

 

It works perfect. Thank you for your help. The formula are implemented as "measures" and then I use them as calculated columns. 

 

Kind Regards,

 

Mohamed.

 

Hello Xiaoxin Sheng,

 

Sorry for late answer, I just came back to work and to PowerBI tests. Thank you for your feedback. I will test this asap and share feedback with you. Could you please explain a bit the formula to be sure I understand.

 

Kind Regards,

 

Mohamed.

 

 screenshot PowerBI.png

 Hello Xiaoxin,

 

Here is the result I get. The columns 6 and 8 should have the same values as I used the formula on the last column. 

I tried to change a few things but nothing gives the right result (column 6).

 

 

Kind Regards,

 

Mohamed.

 

Hello,

 

The formula are actually measures if I understand correctly. It works fine.

 

Thank you for your help.

 

Kind regards,

 

Mohamed.

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.