Forum Discussion

i_stanescu_ro's avatar
2 years ago
Solved

Graph with multiple criteria and comparisons.

Hi guys   I need to create one single graph with the following things:   evolution on months on   sales actual for multiple categories of products sales target for multiple categories of produ...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi i_stanescu_ro 

     

    Please try this:

    First pf all, I create a set of sample data:

    Then add a new table:

    Table 2 = {"now","Last year"}

    Two measures created:

    actualSales =
    VAR _newtable =
        SELECTEDVALUE ( 'Table 2'[Value] )
    VAR _currentDate =
        MAX ( 'Table'[date] )
    VAR _currentProduct =
        SELECTEDVALUE ( 'Table'[Products] )
    RETURN
        SWITCH (
            _newtable,
            "now",
                CALCULATE (
                    SUM ( 'Table'[actual] ),
                    FILTER (
                        ALLSELECTED ( 'Table' ),
                        YEAR ( 'Table'[date] ) = YEAR ( _currentDate )
                            && MONTH ( 'Table'[date] ) = MONTH ( _currentDate )
                            && 'Table'[Products] = _currentProduct
                    )
                ),
            "Last year",
                CALCULATE (
                    SUM ( 'Table'[actual] ),
                    FILTER (
                        ALL ( 'Table' ),
                        YEAR ( 'Table'[date] )
                            = YEAR ( _currentDate ) - 1
                            && MONTH ( 'Table'[date] ) = MONTH ( _currentDate )
                            && 'Table'[Products] = _currentProduct
                    )
                )
        )
    
    targetSales =
    VAR _newtable =
        SELECTEDVALUE ( 'Table 2'[Value] )
    VAR _currentDate =
        MAX ( 'Table'[date] )
    VAR _currentProduct =
        SELECTEDVALUE ( 'Table'[Products] )
    RETURN
        SWITCH (
            _newtable,
            "now",
                CALCULATE (
                    SUM ( 'Table'[Target] ),
                    FILTER (
                        ALLSELECTED ( 'Table' ),
                        YEAR ( 'Table'[date] ) = YEAR ( _currentDate )
                            && MONTH ( 'Table'[date] ) = MONTH ( _currentDate )
                            && 'Table'[Products] = _currentProduct
                    )
                ),
            "Last year",
                CALCULATE (
                    SUM ( 'Table'[Target] ),
                    FILTER (
                        ALL ( 'Table' ),
                        YEAR ( 'Table'[date] )
                            = YEAR ( _currentDate ) - 1
                            && MONTH ( 'Table'[date] ) = MONTH ( _currentDate )
                            && 'Table'[Products] = _currentProduct
                    )
                )
        )
    

    Then add two slicer with the filed of the products and date separately:

    A line and clustered column chart can be created:

     

    The result is as follow:

     

    If the above one can't help you, could you please provide more raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples? It would be helpful to find out the solution. You can refer the following links to share the required info:

    How to provide sample data in the Power BI Forum

    How to Get Your Question Answered Quickly

    And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

    How to upload PBI in Community

    Best Regards

    Zhengdong Xu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.