Forum Discussion

admin_xlsior's avatar
admin_xlsior
Post Prodigy
5 years ago
Solved

Need help on Calculate function

Hi guys,   Need some 'enlightment'. As I have these tables and these relationship :   If I want to get numbers of work orders with this Calculate function, is this correct? Work orders c...
  • PaulDBrown's avatar
    PaulDBrown
    5 years ago

    admin_xlsior 

    Understanding how CALCULATE works is fundamental in DAX. CALCULATE allows you to remove the filter context - if you need to - and apply further filters. The function brekas down into "two sections": 

    CALCULATE( expression, filters)

    Expression is what you are calculating; filters is the rows you want the calculation to be carried out (using filters or table references),

    In you case, your measure is:

    Work orders count = CALCULATE(
    COUNTROWS('Work orders'),
    'Timesheet'
    )

    So the expression in red is what you want to calculate, and the table reference is the filter you want the calculation to be applied to (in other words, which rows to make the calculation on).

    In your model, the table 'Work orders" is a dimension table. If you simply counted the rows of this table, you would get the toal number of rows in the table for all the years, since it is unrleated to the Date table. By adding the "Timesheet" filter expression in the CALCULATE function, you are saying you want the COUNTROWS to be filtered according to the filters applied to the Timesheet table (which is filtered by the Year field.

    To see the difference I've used a sample dataset similar to your structure. This is the model:

     

    Now compare these two measures:

     

    Countrows Dim Items = COUNTROWS('Dim Item')

     

    and (similar to your measure)

     

    Calc Countrows w DataTable = CALCULATE(COUNTROWS('Dim Item'), 
                                    'DataTable')

     

    In a table you get this:

     

    The first measure (your example), is counting the rows filtered by the Year field.

     

    Just so that you can see the DISTINCTCOUNT also:

     

    I hope that helps a bit. It is very important that you understand how CALCULATE works!