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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
ahmer_malick
Helper II
Helper II

Need Help with Dax Formula

I am trying to create a Index . The criteria should we If Order Type is Equal to On Hand then 1 , Safety Stock 2. . Except these 2 criteria, if there is a sales order or a work order after this, it should count it in an asc order even its a past due order ( less than today)

 

RANKX (
    FILTER (
        'SUPPLY & DEMAND',
       
        'SUPPLY & DEMAND'[ITEM NUMBER] = EARLIER ( 'SUPPLY & DEMAND'[ITEM NUMBER] ) && 'SUPPLY & DEMAND'[WHS]=EARLIER('SUPPLY & DEMAND'[WHS])
    ),
    'SUPPLY & DEMAND'[SCHED PICK].[Date],
    , ASC
    , DENSE
)

ahmer_malick_0-1762203304352.png

 

1 ACCEPTED SOLUTION
kushanNa
Super User
Super User

Hi @ahmer_malick 

 

Do you want to keep the index fixed once it has been assigned to an order type? If that’s the case, please check whether this M code (Query1) approach works for you instead of DAX

let
    // --- Source Table ---
    Source = #"SUPPLY & DEMAND",

    // --- Sort to make results consistent ---
    #"Sorted Rows" = Table.Sort(Source, {
        {"ITEM NUMBER", Order.Ascending},
        {"WHS", Order.Ascending},
        {"SCHED PICK", Order.Ascending}
    }),

    // --- Assign base indexes for fixed order types ---
    #"Added BaseIndex" = Table.AddColumn(
        #"Sorted Rows",
        "BaseIndex",
        each 
            if [ORDER TYPE] = "ON HAND BALANCE" then 1
            else if [ORDER TYPE] = "SAFETY STOCKS" then 2
            else null,
        Int64.Type
    ),

    // --- Create distinct list of remaining order types per ITEM+WHS ---
    #"DistinctOrderTypes" = 
        Table.Distinct(
            Table.SelectColumns(
                Table.SelectRows(#"Added BaseIndex", each [BaseIndex] = null),
                {"ITEM NUMBER", "WHS", "ORDER TYPE"}
            )
        ),

    // --- Add index starting from 3 for other order types ---
    #"Add Type Index" =
        Table.Group(
            #"DistinctOrderTypes",
            {"ITEM NUMBER", "WHS"},
            {
                {"TypeList",
                    each 
                        Table.AddIndexColumn(_, "TypeIndex", 3, 1, Int64.Type)
                }
            }
        ),

    // --- Expand TypeIndex for each group ---
    #"Expanded Types" = Table.ExpandTableColumn(#"Add Type Index", "TypeList", {"ORDER TYPE", "TypeIndex"}),

    // --- Join back to main table ---
    #"Merged Index" = Table.NestedJoin(
        #"Added BaseIndex",
        {"ITEM NUMBER", "WHS", "ORDER TYPE"},
        #"Expanded Types",
        {"ITEM NUMBER", "WHS", "ORDER TYPE"},
        "JoinIndex",
        JoinKind.LeftOuter
    ),

    #"Expanded Join" = Table.ExpandTableColumn(#"Merged Index", "JoinIndex", {"TypeIndex"}, {"TypeIndex"}),

    // --- Final GroupedIndex column ---
    #"Added GroupedIndex" = Table.AddColumn(
        #"Expanded Join",
        "GroupedIndex",
        each if [BaseIndex] <> null then [BaseIndex] else [TypeIndex],
        Int64.Type
    ),

    // --- Cleanup ---
    #"Removed Extra Columns" = Table.RemoveColumns(#"Added GroupedIndex", {"BaseIndex", "TypeIndex"})
in
    #"Removed Extra Columns"

 

I have attached the sample pbix file for your reference 

View solution in original post

4 REPLIES 4
v-sgandrathi
Community Support
Community Support

Hi @ahmer_malick,

 

Just wanted to follow up and confirm that everything has been going well on this. Please let me know if there’s anything from our end.
Please feel free to reach out Microsoft fabric community forum.

v-sgandrathi
Community Support
Community Support

Hi @ahmer_malick,

 

Has your issue been resolved?
If the response provided by @ryan_mayu  and @kushanNa  addressed your query, could you please confirm? It helps us ensure that the solutions provided are effective and beneficial for everyone.

Thank you for your understanding!

ryan_mayu
Super User
Super User

@ahmer_malick 

could you pls paste the data here (not the screenshot ) and also provide the expected output?





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




kushanNa
Super User
Super User

Hi @ahmer_malick 

 

Do you want to keep the index fixed once it has been assigned to an order type? If that’s the case, please check whether this M code (Query1) approach works for you instead of DAX

let
    // --- Source Table ---
    Source = #"SUPPLY & DEMAND",

    // --- Sort to make results consistent ---
    #"Sorted Rows" = Table.Sort(Source, {
        {"ITEM NUMBER", Order.Ascending},
        {"WHS", Order.Ascending},
        {"SCHED PICK", Order.Ascending}
    }),

    // --- Assign base indexes for fixed order types ---
    #"Added BaseIndex" = Table.AddColumn(
        #"Sorted Rows",
        "BaseIndex",
        each 
            if [ORDER TYPE] = "ON HAND BALANCE" then 1
            else if [ORDER TYPE] = "SAFETY STOCKS" then 2
            else null,
        Int64.Type
    ),

    // --- Create distinct list of remaining order types per ITEM+WHS ---
    #"DistinctOrderTypes" = 
        Table.Distinct(
            Table.SelectColumns(
                Table.SelectRows(#"Added BaseIndex", each [BaseIndex] = null),
                {"ITEM NUMBER", "WHS", "ORDER TYPE"}
            )
        ),

    // --- Add index starting from 3 for other order types ---
    #"Add Type Index" =
        Table.Group(
            #"DistinctOrderTypes",
            {"ITEM NUMBER", "WHS"},
            {
                {"TypeList",
                    each 
                        Table.AddIndexColumn(_, "TypeIndex", 3, 1, Int64.Type)
                }
            }
        ),

    // --- Expand TypeIndex for each group ---
    #"Expanded Types" = Table.ExpandTableColumn(#"Add Type Index", "TypeList", {"ORDER TYPE", "TypeIndex"}),

    // --- Join back to main table ---
    #"Merged Index" = Table.NestedJoin(
        #"Added BaseIndex",
        {"ITEM NUMBER", "WHS", "ORDER TYPE"},
        #"Expanded Types",
        {"ITEM NUMBER", "WHS", "ORDER TYPE"},
        "JoinIndex",
        JoinKind.LeftOuter
    ),

    #"Expanded Join" = Table.ExpandTableColumn(#"Merged Index", "JoinIndex", {"TypeIndex"}, {"TypeIndex"}),

    // --- Final GroupedIndex column ---
    #"Added GroupedIndex" = Table.AddColumn(
        #"Expanded Join",
        "GroupedIndex",
        each if [BaseIndex] <> null then [BaseIndex] else [TypeIndex],
        Int64.Type
    ),

    // --- Cleanup ---
    #"Removed Extra Columns" = Table.RemoveColumns(#"Added GroupedIndex", {"BaseIndex", "TypeIndex"})
in
    #"Removed Extra Columns"

 

I have attached the sample pbix file for your reference 

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.