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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
ClaudioF
Helper II
Helper II

combinatorics in dinamic dax

Hello guys, i have posted here so many times about getting a logic right, but i just noticed it was impossible, in the way i was thinking, no one could help me.

I have a new aproach of how to get the desired result, its by using combinatorics, of course, i need to know if its even possible in the dax.

I have the image bellow that shows how the combinations might work, and ahead i also have the code i'm trying, its not working properly and i would like some help with it.

Captura de tela 2025-03-19 154453.png

 

In this code, the objective is have at the end the combinations that attend to the condition that the amount of concatenated orders is the highest and the sum of values are <= 21.

 

Obs. this code might work with many different products, but to this example i just used one.

Code:

 

FinalCorrectStatus =
VAR CurrentItem = SELECTEDVALUE('DB_ORDERS'[Produto])
VAR OrdersTable = FILTER(ALL('DB_ORDERS'), 'DB_ORDERS'[Produto] = CurrentItem)

VAR OrderCombinations =
ADDCOLUMNS(
GENERATEALL(
OrdersTable,
SUMMARIZE(
OrdersTable,
'DB_ORDERS'[Pedido],
"Soma_Quant_Falta", SUM('DB_ORDERS'[QT. falt])
)
),
"PedidosConcatenados", CONCATENATEX(OrdersTable, 'DB_ORDERS'[Pedido], ", "),
"Qtd_Pedidos", COUNTROWS(OrdersTable) -- Conta o número de pedidos na combinação
)

VAR FilteredCombinations =
FILTER(OrderCombinations, [Soma_Quant_Falta] <= 21)

VAR MaxPedidos = MAXX(FilteredCombinations, [Qtd_Pedidos]) -- Encontra o maior número de pedidos

VAR BestCombination =
FILTER(FilteredCombinations, [Qtd_Pedidos] = MaxPedidos) -- Mantém apenas a combinação com mais pedidos

RETURN BestCombination

 

Can anyone please help me with it?

4 REPLIES 4
VahidDM
Super User
Super User

Hi @ClaudioF 

 

Try this:

BestCombination =
VAR CurrentProduct = SELECTEDVALUE('DB_ORDERS'[Produto])

VAR OrdersTable =
    FILTER(
        ALL('DB_ORDERS'),
        'DB_ORDERS'[Produto] = CurrentProduct
    )

VAR AllCombos =
    GENERATE(
        OrdersTable,
        FILTER(
            OrdersTable,
            'DB_ORDERS'[Pedido] <= EARLIER('DB_ORDERS'[Pedido])
        )
    )

VAR CombosWithSum =
    ADDCOLUMNS(
        AllCombos,
        "PedidosConcatenados", CONCATENATEX(
            FILTER(
                OrdersTable,
                'DB_ORDERS'[Pedido] <= EARLIER('DB_ORDERS'[Pedido])
            ),
            'DB_ORDERS'[Pedido],
            ", ",
            'DB_ORDERS'[Pedido], ASC
        ),
        "Soma_Quant_Falta", SUMX(
            FILTER(
                OrdersTable,
                'DB_ORDERS'[Pedido] <= EARLIER('DB_ORDERS'[Pedido])
            ),
            'DB_ORDERS'[QT. falt]
        ),
        "Qtd_Pedidos", COUNTROWS(
            FILTER(
                OrdersTable,
                'DB_ORDERS'[Pedido] <= EARLIER('DB_ORDERS'[Pedido])
            )
        )
    )

VAR FilteredCombos =
    FILTER(
        CombosWithSum,
        [Soma_Quant_Falta] <= 21
    )

VAR MaxQtdPedidos =
    MAXX(FilteredCombos, [Qtd_Pedidos])

VAR BestComboFinal =
    TOPN(
        1,
        FILTER(FilteredCombos, [Qtd_Pedidos] = MaxQtdPedidos),
        [Soma_Quant_Falta], DESC
    )

RETURN
    CONCATENATEX(
        BestComboFinal,
        [PedidosConcatenados],
        "; "
    )

 

Generate combinations by comparing each order to orders with equal or lower numbers to simplify calculations.
Calculate the sum (Soma_Quant_Falta) and count (Qtd_Pedidos) for each combination.
Filter only combinations whose sum is <= 21.
Pick the combination with the maximum number of orders (Qtd_Pedidos).
Finally, concatenate the order numbers clearly for display.

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.

Appreciate your Kudos!! 

 

LinkedIn|Twitter|Blog |YouTube 

Hey there! thankyou for your return;

This code you provided is generating an error: The GENERATE function does not allow two columns with the same name "DB_ORDERS"[Cliente]. i got this same problem in other attempts..

You right 🙂
Try this:

BestCombination =
VAR CurrentProduct = SELECTEDVALUE('DB_ORDERS'[Produto])

VAR OrdersTable =
    FILTER(
        ALL('DB_ORDERS'),
        'DB_ORDERS'[Produto] = CurrentProduct
    )

VAR OrdersDistinct =
    SELECTCOLUMNS(
        OrdersTable,
        "Pedido", 'DB_ORDERS'[Pedido],
        "QT_falt", 'DB_ORDERS'[QT. falt]
    )

VAR Combos =
    GENERATE(
        OrdersDistinct,
        FILTER(
            OrdersDistinct,
            [Pedido] <= EARLIER([Pedido])
        )
    )

VAR ComboSummary =
    ADDCOLUMNS(
        Combos,
        "PedidosConcatenados",
            CONCATENATEX(
                FILTER(OrdersDistinct, [Pedido] <= EARLIER([Pedido])),
                [Pedido],
                ", ",
                [Pedido], ASC
            ),
        "Soma_Quant_Falta",
            SUMX(
                FILTER(OrdersDistinct, [Pedido] <= EARLIER([Pedido])),
                [QT_falt]
            ),
        "Qtd_Pedidos",
            COUNTROWS(
                FILTER(OrdersDistinct, [Pedido] <= EARLIER([Pedido]))
            )
    )

VAR FilteredCombos =
    FILTER(
        ComboSummary,
        [Soma_Quant_Falta] <= 21
    )

VAR MaxQtdPedidos =
    MAXX(FilteredCombos, [Qtd_Pedidos])

VAR BestCombo =
    TOPN(
        1,
        FILTER(FilteredCombos, [Qtd_Pedidos] = MaxQtdPedidos),
        [Soma_Quant_Falta], DESC
    )

RETURN
    CONCATENATEX(
        BestCombo,
        [PedidosConcatenados],
        "; "
    )

didnt work.. i think its not even possible though, i am thinking about changing to python completlty in order to achieve this result i need

Helpful resources

Announcements
March PBI video - carousel

Power BI Monthly Update - March 2025

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

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.

"); $(".slidesjs-pagination" ).prependTo(".pagination_sec"); $(".slidesjs-pagination" ).append("
"); $(".slidesjs-play.slidesjs-navigation").appendTo(".playpause_sec"); $(".slidesjs-stop.slidesjs-navigation").appendTo(".playpause_sec"); $(".slidesjs-pagination" ).append(""); $(".slidesjs-pagination" ).append(""); } catch(e){ } /* End: This code is added by iTalent as part of iTrack COMPL-455 */ $(".slidesjs-previous.slidesjs-navigation").attr('tabindex', '0'); $(".slidesjs-next.slidesjs-navigation").attr('tabindex', '0'); /* start: This code is added by iTalent as part of iTrack 1859082 */ $('.slidesjs-play.slidesjs-navigation').attr('id','playtitle'); $('.slidesjs-stop.slidesjs-navigation').attr('id','stoptitle'); $('.slidesjs-play.slidesjs-navigation').attr('role','tab'); $('.slidesjs-stop.slidesjs-navigation').attr('role','tab'); $('.slidesjs-play.slidesjs-navigation').attr('aria-describedby','tip1'); $('.slidesjs-stop.slidesjs-navigation').attr('aria-describedby','tip2'); /* End: This code is added by iTalent as part of iTrack 1859082 */ }); $(document).ready(function() { if($("#slides .item").length < 2 ) { /* Fixing Single Slide click issue (commented following code)*/ // $(".item").css("left","0px"); $(".item.slidesjs-slide").attr('style', 'left:0px !important'); $(".slidesjs-stop.slidesjs-navigation").trigger('click'); $(".slidesjs-previous").css("display", "none"); $(".slidesjs-next").css("display", "none"); } var items_length = $(".item.slidesjs-slide").length; $(".slidesjs-pagination-item > button").attr("aria-setsize",items_length); $(".slidesjs-next, .slidesjs-pagination-item button").attr("tabindex","-1"); $(".slidesjs-pagination-item button").attr("role", "tab"); $(".slidesjs-previous").attr("tabindex","-1"); $(".slidesjs-next").attr("aria-hidden","true"); $(".slidesjs-previous").attr("aria-hidden","true"); $(".slidesjs-next").attr("aria-label","Next"); $(".slidesjs-previous").attr("aria-label","Previous"); //$(".slidesjs-stop.slidesjs-navigation").attr("role","button"); //$(".slidesjs-play.slidesjs-navigation").attr("role","button"); $(".slidesjs-pagination").attr("role","tablist").attr("aria-busy","true"); $("li.slidesjs-pagination-item").attr("role","list"); $(".item.slidesjs-slide").attr("tabindex","-1"); $(".item.slidesjs-slide").attr("aria-label","item"); /*$(".slidesjs-stop.slidesjs-navigation").on('click', function() { var itemNumber = parseInt($('.slidesjs-pagination-item > a.active').attr('data-slidesjs-item')); $($('.item.slidesjs-slide')[itemNumber]).find('.c-call-to-action').attr('tabindex', '0'); });*/ $(".slidesjs-stop.slidesjs-navigation, .slidesjs-pagination-item > button").on('click keydown', function() { $.each($('.item.slidesjs-slide'),function(i,el){ $(el).find('.c-call-to-action').attr('tabindex', '-1'); }); var itemNumber = parseInt($('.slidesjs-pagination-item > button.active').attr('data-slidesjs-item')); $($('.item.slidesjs-slide')[itemNumber]).find('.c-call-to-action').attr('tabindex', '0'); }); $(".slidesjs-play.slidesjs-navigation").on('click', function() { $.each($('.item.slidesjs-slide'),function(i,el){ $(el).find('.c-call-to-action').attr('tabindex', '-1'); }); }); $(".slidesjs-pagination-item button").keyup(function(e){ var keyCode = e.keyCode || e.which; if (keyCode == 9) { e.preventDefault(); $(".slidesjs-stop.slidesjs-navigation").trigger('click').blur(); $("button.active").focus(); } }); $(".slidesjs-play").on("click",function (event) { if (event.handleObj.type === "click") { $(".slidesjs-stop").focus(); } else if(event.handleObj.type === "keydown"){ if (event.which === 13 && $(event.target).hasClass("slidesjs-play")) { $(".slidesjs-stop").focus(); } } }); $(".slidesjs-stop").on("click",function (event) { if (event.handleObj.type === "click") { $(".slidesjs-play").focus(); } else if(event.handleObj.type === "keydown"){ if (event.which === 13 && $(event.target).hasClass("slidesjs-stop")) { $(".slidesjs-play").focus(); } } }); $(".slidesjs-pagination-item").keydown(function(e){ switch (e.which){ case 37: //left arrow key $(".slidesjs-previous.slidesjs-navigation").trigger('click'); e.preventDefault(); break; case 39: //right arrow key $(".slidesjs-next.slidesjs-navigation").trigger('click'); e.preventDefault(); break; default: return; } $(".slidesjs-pagination-item button.active").focus(); }); }); // Start This code is added by iTalent as part of iTrack 1859082 $(document).ready(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); $("#tip2").attr("aria-hidden","true").addClass("hidden"); $(".slidesjs-stop.slidesjs-navigation, .slidesjs-play.slidesjs-navigation").attr('title', ''); $("a#playtitle").focus(function(){ $("#tip1").attr("aria-hidden","false").removeClass("hidden"); }); $("a#playtitle").mouseover(function(){ $("#tip1").attr("aria-hidden","false").removeClass("hidden"); }); $("a#playtitle").blur(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); }); $("a#playtitle").mouseleave(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); }); $("a#play").keydown(function(ev){ if (ev.which ==27) { $("#tip1").attr("aria-hidden","true").addClass("hidden"); ev.preventDefault(); return false; } }); $("a#stoptitle").focus(function(){ $("#tip2").attr("aria-hidden","false").removeClass("hidden"); }); $("a#stoptitle").mouseover(function(){ $("#tip2").attr("aria-hidden","false").removeClass("hidden"); }); $("a#stoptitle").blur(function(){ $("#tip2").attr("aria-hidden","true").addClass("hidden"); }); $("a#stoptitle").mouseleave(function(){ $("#tip2").attr("aria-hidden","true").addClass("hidden"); }); $("a#stoptitle").keydown(function(ev){ if (ev.which ==27) { $("#tip2").attr("aria-hidden","true").addClass("hidden"); ev.preventDefault(); return false; } }); }); // End This code is added by iTalent as part of iTrack 1859082
Top Kudoed Authors