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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
aar0n
Advocate II
Advocate II

How to create an Index column in Power Query based on 4 columns, and conditional criteria

Hi Guys, I'm new to Power Query and DAX, and i need to create a column in power query that makes a 'planned order'.

so far, i have sorted my columns based on "ID" and "Location", and then used an index function that resets itself every time a new Location is shown..  but i also need to factor in the "chance of success", as well as the "type"

 

What i need is:  if the well is "Type" - V, and if the chance of success is less than 50, and the COS is less than the next "Location" --show 0, and for the next Location, show 1, and so on and so forth..

 

 

However,  i also have another "Type" column, that can show one of 3 values: a blank value, H, or V. 

if there is a blank value in "Type", there is always a 0 in the planned order,

H values are always 1 in the planned order,

and V values follow the "chance of success" rules outlined above...

 

 

an example of what i need for the planned order column is shown below. 

 

 

doing this in the query editor would be ideal (to use it in other query manipulations after this is done), but a DAX function can work too.. 

IDLocationTYPEPlanned OrderChance of Success (%)
100c 025
100bV180
100aV275
200bV150
200aV270
300dH180
300cV170
300bV250
300aV350
6 REPLIES 6
Phil_Seamark
Employee
Employee

HI @aar0n

 

This DAX calculated column solves your problem based on your sample data.  But doesn't take into account anything to do with the [Chance of Success] field.  Your description didn't make sense with your sample data/output.  

 

Can you tweak your data this doesn't do what you need?

 

Planned Order = 
VAR x  = 
    CALCULATE(
           COUNTROWS('Table1') ,
           FILTER(
               'Table1',
               'Table1'[ID] = EARLIER('Table1'[ID])
               && 'Table1'[TYPE] = "V"
               && 'Table1'[Location] > EARLIER('Table1'[Location])
           )
     )+1
RETURN 
    SWITCH(
        TRUE(),
        'Table1'[TYPE] = "H" , 1 ,
        'Table1'[TYPE] = "" , 0 , 
        x
        )

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

Hi, 

 

here are 2 more scenarios for a sample of the data i have.

 

IDLocationTYPEChance of Success (%)Planned Order (without including the COS)Planned Order (Including COS)
400gV4010
400fV7521
400eV5032
400dV7043
400cV7054
400bV5065
400aV5076

 

Here is another example:

IDLocationTYPEChance of Success (%)Planned Order (without including the COS)Planned Order (Including COS)
500eV5011
500dV6622
500cV4530
500bV8043
500aV2050

 

This Planned order (including COS)  only  applies to "type" V

the data is all in one table.. with about 4000 rows..  i'm just showing an example of what the final stage needs to look like

 

Hi @aar0n

 

I guess it just might be helpful to show extra scenarios in your sample data that demonstrate what you are after.


To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

i've attached a better example..

Hi @aar0n,

I am still confuse about the calculation rules of Planned Order (without including the COS)  and Planned Order (Including COS), you should describe in details. In addition, there is only one issue in one thread, you should mark the right reply as answer if your original thread has been resolved, and please create another new thread if you have new issues.

Thanks,
Angelia

i apologize for the confusion Angelia,

 

my original question wasnt fully answered.

 

basically, @Phil_Seamark 's answer is almost correct. what i'm looking for will have a similar structure to Phil's answer, with the addition of something similar to what is shown in red : 

 

 

i want the formula to add 1 to the formula for the "V" types, only if the COS>Earlier COS, or if COS>50 , otherwise i want the formula to show 0. 

 

 

Planned Order = 
VAR x  = 
    CALCULATE(
           COUNTROWS('Table1') ,
           FILTER(
               'Table1',
               'Table1'[ID] = EARLIER('Table1'[ID])
               && 'Table1'[TYPE] = "V"
               && 'Table1'[Location] > EARLIER('Table1'[Location])
           )
     )+1 if (COS > EARLIER(COS)) or (COS > 50) 
Else show 0 for that particular row







RETURN SWITCH( TRUE(), 'Table1'[TYPE] = "H" , 1 , 'Table1'[TYPE] = "" , 0 , x )

 

 

Below, i am using one of my previous examples i posted, and im showing how i would do the formula in excel - for One "ID" that is only type "V".

 

 

column I and column F are the same, the only reason theyre being shown twice here is to show that the end result is the same.

column g and h are only being used as "helper columns".

 

 

**the formula in G,H, and I shows the formula for the row directly below.

ABCDEFG  HI
IDLocationTYPEChance of Success (%)Planned Order (without including the COS)Planned Order (Including COS)

IF(OR(IF(D2>=50,1,0),

IF(D2>D1,1,0)),1,0)

SUM($G$2:G2)IF(H2=H1,0,H2)
500eV50111  11
500dV6622122
500cV4530020
500bV8043133
500aV2050030

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.