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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

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
Anonymous
Not applicable

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

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.

Anonymous
Not applicable

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

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
Anonymous
Not applicable

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

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.