Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

How can I create this table or matrix?

Hi community,  It's the second post I do for this. I'm very obfuscated 'cause I can't solve this yet... I hope somebody can help me. I have two tables; Table A Item Request OnSite Pcs ...
  • edhans's avatar
    6 years ago

    See if this works for you. It returns a text box (scalar, or single, value) that is a field for a table.

     

    This is the code behind the "Pallets to Get" measure.

     

    Pallets to Get = 
    VAR varQuantityNeeded =
        MAX( 'Table A'[Pcs to complete request] )
    VAR varCurrentItem =
        MAX( 'Table A'[Item] )
    VAR varPalletCumulative =
        ADDCOLUMNS(
            FILTER(
                'Table B',
                'Table B'[Item] = varCurrentItem
            ),
            "Cumulative Pcs", CALCULATE(
                SUM( 'Table B'[Pallet Pcs] ),
                VAR varCurrentPalletID =
                    CALCULATE(
                        MAX( 'Table B'[Pallet ID] )
                    )
                RETURN
                    FILTER(
                        'Table B',
                        'Table B'[Item] = varCurrentItem
                            && 'Table B'[Pallet ID] <= varCurrentPalletID
                    )
            )
        )
    VAR varLastPalletID =
        MINX(
            FILTER(
                varPalletCumulative,
                [Cumulative Pcs] > varQuantityNeeded
            ),
            [Pallet ID]
        )
    VAR varFinalTable =
        FILTER(
            varPalletCumulative,
            [Pallet ID] <= varLastPalletID
        )
    VAR Result =
        CONCATENATEX(
            varFinalTable,
            "Pallet " & [Pallet ID] & ": " & [Pallet Pcs] & "pcs",
            ","
                & UNICHAR( 10 )
        )
    RETURN
        Result

     

    The varFinalTable variable (next to the last variable) returns this table in memory:

    So the trick is how to get that into a visual. A measure must be a scalar value, and I cannot think of a good way to do it otherwise. I could create a calculated table, but that is on a per item basis. 

    I might redo this in Power Query that will return similar tables that are loaded, but this will get you something at least. And if you have a lot of data, the Power Query solution would be really slow. I will have to tinker with it.