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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Excel and Power BI Query

Site NameTransaction_EndSales_Mix_IndPayment_MethodPayment_method_StdFuel or CRFuelCRESCard
Sedgemoor South20/05/2018 00:09FEuroshell CRT 2ES     
Sedgemoor South20/05/2018 00:09CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 00:09CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 00:55FMastercardcard00:4500:46   
Sedgemoor South20/05/2018 01:00FBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 01:46FEuroshellES00:4500:45 01:37 
Sedgemoor South20/05/2018 02:50FBritish Pounds Sterlingcash01:0301:03   
Sedgemoor South20/05/2018 03:30FVISAcard00:4000:40  02:34
Sedgemoor South20/05/2018 05:09FEuroshell CRTES01:3901:39 03:23 
Sedgemoor South20/05/2018 05:29CVISAcard  05:19  
Sedgemoor South20/05/2018 05:29CVISAcard    01:59
Sedgemoor South20/05/2018 05:38FBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 05:50CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 05:50CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 05:50CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 06:16CBritish Pounds Sterlingcash     
Sedgemoor South20/05/2018 06:18CVISAcard    00:49
Sedgemoor South20/05/2018 06:18CVISAcard     
Sedgemoor South20/05/2018 06:18CVISAcard     
Sedgemoor South20/05/2018 06:32FAllStarcard 00:54   
Sedgemoor South20/05/2018 06:33CVISAcard     

 

Need help in automatically calculating the following columns in Excel and Power BI

 

 

The columns are "Fuel or CR", "Fuel", "CR", "ES", Card". In this example, I have manually calculated them, but I need the formula for the same.

 

 

This is how they need to be calculated


"Fuel or CR": This is straightforward. Difference between two rows of the Transaction_end column

F5 = B5 - B4
F6 = B6 - B5 
and so on and so forth
Note only values greater than 30 mins are mentioned here.

"Fuel",
This depends on the value in the field "Sales_Mix_Ind" depending on whether it is "F"
If its "F", that means Fuel, which means you only substract the corresponding values in the Transaction_end Column
Hence G5 = B5 -B2 as Sales_mix_indicator is "F" in columns C5 and C2
G7 = B6- B5 as Sales_mix_indicator is "F" in columns C6 and C5
Note only values greater than 30 mins are mentioned here.


"CR",

This depends on the value in the field "Sales_Mix_Ind" depending on whether it is "C"
If its "C", that means Fuel, which means you only substract the corresponding values in the Transaction_end Column

Hence H11 = B11 -  B4 as Sales_mix_indicator is "C" in columns C11 and C4
Note only values greater than 30 mins are mentioned here.


"ES",

This depends on the value in the field "Payment_method_std" depending on whether it is "ES"
If its "ES", that means EUroshell, which means you only substract the corresponding values in the Transaction_end Column

Hence I7 = B7 -  B2 as "Payment_method_std"  is "ES" in columns E7 and E2
And I10 = B10 -  B7 as "Payment_method_std"  is "ES" in columns E10 and E7
Note only values greater than 30 mins are mentioned here.


Card".


This depends on the value in the field "Payment_method_std" depending on whether it is "card"
If its "card", that means card payment, which means you only substract the corresponding values in the Transaction_end Column

Hence J9 = B9 -  B5 as "Payment_method_std"  is "card" in columns E9 and E5
And I12 = B12 -  B9 as "Payment_method_std"  is "card" in columns E12 and E9
Note only values greater than 30 mins are mentioned here.

 

 

3 REPLIES 3
Anonymous
Not applicable

Hi @Anonymous,

 

Your table seems contains duplicate records, I'd like to suggest you add a index column to calculate looping table.

 

Calculated column formulas:

Spoiler

 

Fuel or CR = 
VAR previous =
    CALCULATE (
        MAX ( Test[Transaction_End] ),
        FILTER ( ALL ( Test ), [Transaction_End] < EARLIER ( Test[Transaction_End] ) )
    )
VAR totalSecond =
    DATEDIFF ( previous, [Transaction_End], SECOND ) + 0
VAR difftime =
    TIME ( INT ( totalSecond / 3600 ), MOD ( totalSecond, 3600 ) / 60, MOD ( MOD ( totalSecond, 3600 ), 60 ) )
RETURN
    IF ( totalSecond >= 1800, difftime, BLANK () )


Fuel = 
VAR previous =
    CALCULATE (
        MAX ( Test[Transaction_End] ),
        FILTER (
            ALL ( Test ),
            [Index] < EARLIER ( Test[Index] )
                && [Sales_Mix_Ind] = "F"
        )
    )
VAR totalSecond =
    DATEDIFF ( previous, [Transaction_End], SECOND ) + 0
VAR difftime =
    TIME ( INT ( totalSecond / 3600 ), MOD ( totalSecond, 3600 ) / 60, MOD ( MOD ( totalSecond, 3600 ), 60 ) )
RETURN
    IF ( [Sales_Mix_Ind] = "F" && totalSecond >= 1800, difftime, BLANK () )


CR = 
VAR previous =
    CALCULATE (
        MAX ( Test[Transaction_End] ),
        FILTER (
            ALL ( Test ),
            [Index] < EARLIER ( Test[Index] )
                && [Sales_Mix_Ind] = "C"
        )
    )
VAR totalSecond =
    DATEDIFF ( previous, [Transaction_End], SECOND ) + 0
VAR difftime =
    TIME ( INT ( totalSecond / 3600 ), MOD ( totalSecond, 3600 ) / 60, MOD ( MOD ( totalSecond, 3600 ), 60 ) )
RETURN
    IF ( [Sales_Mix_Ind] = "C" && totalSecond >= 1800, difftime, BLANK () )


ES = 
VAR previous =
    CALCULATE (
        MAX ( Test[Transaction_End] ),
        FILTER (
            ALL ( Test ),
            [Index] < EARLIER ( Test[Index] )
                && [Payment_method_Std] = "ES"
        )
    )
VAR totalSecond =
    DATEDIFF ( previous, [Transaction_End], SECOND ) + 0
VAR difftime =
    TIME ( INT ( totalSecond / 3600 ), MOD ( totalSecond, 3600 ) / 60, MOD ( MOD ( totalSecond, 3600 ), 60 ) )
RETURN
    IF ( [Payment_method_Std] = "ES" && totalSecond >= 1800, difftime, BLANK () )


Card = 
VAR previous =
    CALCULATE (
        MAX ( Test[Transaction_End] ),
        FILTER (
            ALL ( Test ),
            [Index] < EARLIER ( Test[Index] )
                && [Payment_method_Std] = "Card"
        )
    )
VAR totalSecond =
    DATEDIFF ( previous, [Transaction_End], SECOND ) + 0
VAR difftime =
    TIME ( INT ( totalSecond / 3600 ), MOD ( totalSecond, 3600 ) / 60, MOD ( MOD ( totalSecond, 3600 ), 60 ) )
RETURN
    IF ( [Payment_method_Std] = "Card" && totalSecond >= 1800, difftime, BLANK () )

 

24.png

 

Regards,

Xiaoxin Sheng

Anonymous
Not applicable

Hi Xiaoxin Sheng

 

     Many Thanks for your reply. Your reply has allowed me to get the desired result on a small subset of the data that I had mentioned. However if I try to apply the same logic on a much bigger data set of 1 month (366856 rows), I am getting an out of memory error. I try to create a new column after applying an index on the data but the column creation "keeps working on it" for a long time and gives me an out of memory error.

Is there a way I can resolve this issue.

 

I have modified your code slightly. In fact I also had devised another method of achieving the same result, before your reply, but both methods are giving me an out of memory error.

 

 


Fuel2 =
VAR previous =
    CALCULATE (
        MAX ( Original[Transaction_End] ),
        FILTER (
            ALL ( Original ),
            [Index] < EARLIER ( Original[Index] )
                && [Sales_Mix_Ind] = "F"
        )
    )
VAR totalminute =
    DATEDIFF ( previous, [Transaction_End], MINUTE ) + 0
RETURN
    IF ( [Sales_Mix_Ind] = "F" && totalminute >= 60, totalminute, BLANK () )

 


LagColumn F = DATEDIFF(Original[Transaction_end]
                , CALCULATE(MAX(Original[Transaction_end]),
                    FILTER(original,original[Sales_Mix_Ind]="F"),
                    FILTER(Original,
                        Original[Sales_Mix_ind] = EARLIER(Original[Sales_Mix_ind]) &&
                        Original[Transaction_end] < EARLIER(Original[Transaction_end])
                    )
                )
            , MINUTE
            )

 

Would be great if you can help me resolve this issue

 

Rgds

Amit

 

 

 

Anonymous
Not applicable

Hi @Anonymous,

 

Which version of power bi desktop you used? If you are working on 32 bit version, I'd like to suggest you use 64 bit to increase application memory limit.(32 bit application has 2G memory usage limit)

 

In addition, your calculation need to looping whole table to compare current and previous records to get diff. It obviously cost lots of memory resource when you use a lot similar calculations formulas.

 

For this scenario, I'd like to suggest you add more conditions to reduce loop range to optimization memory usage.

 

Regards,
Xiaoxin Sheng

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

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.

Top Solution Authors