Forum Discussion

BramSegers's avatar
BramSegers
Frequent Visitor
6 years ago

Matrix with dynamic rows?

I've got a very small dataset (too small to post 😊) that contains just 3 tables: 

Vehicles which contains two columns: Vehicle No. & Standard Department

VehicleStandard Department
DTR-465COIL-ACE
DTR-466COIL-ACE
DTR-467COIL-ACE
DTR-468ACE-ATT
DTR-469ACE-ATT
DTR-470IBERICA


Appointed Department which contains 4 columns: Vehicle No., From Date, Till Date, Appointed Department

VehicleFromTillAppointed Department
DTR-46501/01/202031/01/2020ACE-ATT
DTR-46501/02/202028/02/2020IBERICA
DTR-47001/01/2020 COIL-ACE

 

Turnover which contains 2 columns: Vehicle No., Turnover. 

VehicleTurnover
DTR-465€ 200
DTR-466€ 300
DTR-467€ 250
DTR-468€ 275
DTR-469€ 315
DTR-470€ 180

 

What i want to achieve is to create a matrix table and give the end user the possibilty to filter on a date, filtering my matrixtable immedately with the current correct info.

 

So if the user selects till 30/01/2020 he would get something like:

 Selection:   Selection: 
 30/01/2020   03/02/2020 
       
  Turnover   Turnover
ACE-ATT-790 ACE-ATT-590
 DTR-465200  DTR-468275
 DTR-468275  DTR-469315
 DTR-469315 COIL-ACE-930
COIL-ACE-730  DTR-466300
 DTR-466300  DTR-467250
 DTR-467250  DTR-470180
 DTR-470180  DTR-465200

 

Any suggestions how to achieve this?

11 Replies

  • Hi,

    I do not understand your question.  Vehicles DTR-466,467,468 and 469 have no record at all in the second table.  Therefore, when one selects the date as 30/1/2020, why should these appear in the final result?

    • BramSegers's avatar
      BramSegers
      Frequent Visitor

      Hi,

      Thanks for your reply.

      I indeed didn't mention that, but those vehicles should appear date-independant, because they have a registered turnover.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Refer to the table,

         

        VehicleFromTillAppointed Department
        DTR-46501/01/202031/01/2020ACE-ATT
        DTR-46501/02/202028/02/2020IBERICA
        DTR-47001/01/2020 COIL-ACE

        Here vehicle no DTR-465 is appointed to IBERICA department during the period from 01/02/2020 to 28/02/2020. Then why is vehicle no. DTR-465 appearing in department "COIL-ACE" in your example when the selected date is 03/02/2020?

         

        I need clarification on this to arrive at the logic. Because when I tried the solution, following are the outputs I am getting in Power BI with date selections 30/01/2020 and 03/02/2020.

         

         

         

  • Anonymous's avatar
    Anonymous
    Not applicable

    You may try the following solution. It is not an optimum solution, but a simple one to start with.

     

    Step 1:

    Add a calendar table to your data model. Select "New Table" and use the following DAX code.

     

    Calendar = CALENDAR(DATE(2020,1,1),DATE(2020,2,29))

     

    Change the date ranges as per your requirement. 

    Step 2:

    Add another calculated table to your data model. Select "New Table" and use the following DAX code.

     

    VehicleCalendar = CROSSJOIN(Vehicles,'Calendar')

     

    Step 3:

    Add two calculated columns to the "VehicleCalendar" table that you have added in Step 2.

     

     

    Appointed Department =
    CONCATENATEX (
        FILTER (
            'Appointed Department',
            AND (
                AND (
                    'Appointed Department'[From] <= VehicleCalendar[Date],
                    'Appointed Department'[TillDate] >= VehicleCalendar[Date]
                ),
                'Appointed Department'[Vehicle] = VehicleCalendar[Vehicle]
            )
        ),
        'Appointed Department'[Appointed Department]
    )
    Relevant Department =
    IF (
        ISBLANK ( VehicleCalendar[Appointed Department] ),
        VehicleCalendar[Standard Department],
        VehicleCalendar[Appointed Department]
    )

     

    Step 3a: Add another calculated column to the "Appointed Department" table.

    TillDate = IF(ISBLANK('Appointed Department'[Till]),MAX('Calendar'[Date]),'Appointed Department'[Till])

    Step 4: Add a measure

     

    Turnover = SUMX(Turnover,Turnover[Turnover])

     

     

    The relationships should be as follows...

     

     

    Please pay attention to the Cardinality and Cross Filter Directions of the relationships

     

    Step 5:

    Add a date slicer to your data model using the "Date" field from the "Calendar" table.

     

    Step 6:

    Add a matrix visual and add the "Relevant Department" and "Vehicle" Columns from the table named "Vehicle Calendar" into the Rows of the matrix visual.

     

    Step 7:

    Add the measure named "Turnover " that we have created in Step 4 to the "Values" section of the matrix visual.

     

    This will give the results as shown in my earlier post.

     

    This may not be the most optimum solution, but once you do this, you will get started and you can further improvise.

    • BramSegers's avatar
      BramSegers
      Frequent Visitor

      Thanks a lot for this help, really appreciated!!! 

      The one thing left is that we only have to look at the "Till" ("Tot") date to determine the "Department Appointed".

      So for the below scenario, since 14/02/2020 is in the latest period, only IBERICA should be visible. Is this something we can achieve as well?

      • Anonymous's avatar
        Anonymous
        Not applicable

        If you only need to select the till date only, then you can just change the date selector to "Drop Down" instead of "Between" as shown in the images below. Then set the "Single Select" property of the date slicer to true.