Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Calculation giving the same value against every row

Hello,

 

I want to calculate the total Up Time on the basis of "Hours Of Operational Use". I have written the below DAX measure for the same.

 

vTotalUptime = IF(MAX('Service Availability'[Hours Of Operational Use])="24x7",[vTotalDaysInPeriod]*24*60,
IF(MAX('Service Availability'[Hours Of Operational Use])="Business Hours",[vBusinessDaysInPeriod]*9*60,
IF(MAX('Service Availability'[Hours Of Operational Use])="Extended Hours - 7 days",[vTotalDaysInPeriod]*13*60,
IF(MAX('Service Availability'[Hours Of Operational Use])="Extended Hours - 5 days",[vBusinessDaysInPeriod]*13*60,""))))
 
However, the output is giving me the same value against every row which is not correct. 
The output is correct only for the first row i.e. 31*24*60 = 44640
However, 2nd row should give me the value as 540 i.e. 1*9*60 = 540. Similarly 3rd row should show 1*13*60 =780 & 4th row should show 31*13*60 = 24180
Can someone please highlight what have I done wrong above.
 
Thanks.
 
  • Anonymous's avatar
    Anonymous
    3 years ago

    Thanks tamerj1  for your time and effort to get this calculation correct. 

    What I discovered later is that the problem was not with the DAX witten by me. I was making use of column 'Hours of Operational Use' from wrong table which was causing this problem. After changing the table, the same DAX started giving the correct output. Please see below snapshot. 

    But I must admit that your modified DAX version is simple and easy to understand.

    Thanks Again for your quick help. Much appreciated. Cheers.

4 Replies

  • tamerj1's avatar
    tamerj1
    Icon for Community Champion rankCommunity Champion

    HI Anonymous 
    Apparently you have created a calculated column instead of a measure. Just use the same DAX in a measure and it should wok. However, here is an improved version of the DAX

    vTotalUptime =
    VAR Days = [vTotalDaysInPeriod]
    VAR BusinessDays = [vBusinessDaysInPeriod]
    RETURN
        SUMX (
            VALUES ( 'Service Availability'[Hours Of Operational Use] ),
            SWITCH (
                'Service Availability'[Hours Of Operational Use],
                "24x7",
                    Days * 24 * 60,
                "Business Hours",
                    BusinessDays * 9 * 60,
                "Extended Hours - 7 days",
                    Days * 13 * 60,
                "Extended Hours - 5 days",
                    BusinessDays * 13 * 60
            )
        )

    Make sure [vTotalDaysInPeriod] and [vTotalDaysInPeriod] are also measures not calculated columns. Please let me know if they are in fact columns in the source data table.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks tamerj1  for your time and effort to get this calculation correct. 

      What I discovered later is that the problem was not with the DAX witten by me. I was making use of column 'Hours of Operational Use' from wrong table which was causing this problem. After changing the table, the same DAX started giving the correct output. Please see below snapshot. 

      But I must admit that your modified DAX version is simple and easy to understand.

      Thanks Again for your quick help. Much appreciated. Cheers.

  • some_bih's avatar
    some_bih
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous  your measure vTotalUptime, could be as shown belos adjust Sheet3 to your table name

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
    vTotalUptime =
    VAR _selected_value =
        SELECTEDVALUE ( Sheet3[Hours Of Operational Use] )
    VAR _totaldaysinperiod =
        SELECTEDVALUE ( Sheet3[vTotalDaysInPeriod] )
    VAR _businessdaysinperiod =
        SELECTEDVALUE ( Sheet3[vBusinessDaysInPeriod] )
    VAR _factor_hours_9 = 9
    VAR _factor_hours_13 = 13
    VAR _factor_hours_24 = 24
    VAR _factor_minutes = 60
    RETURN
        IF (
            _selected_value = "24x7",
            _totaldaysinperiod * _factor_hours_24 * _factor_minutes,
            IF (
                _selected_value = "Business Hours",
                _businessdaysinperiod * _factor_hours_9 * _factor_minutes,
                IF (
                    _selected_value = "Extended Hours - 7 days",
                    _totaldaysinperiod * _factor_hours_13 * _factor_minutes,
                    IF (
                        _selected_value = "Extended Hours - 5 days",
                        _businessdaysinperiod * _factor_hours_13 * _factor_minutes
                    )
                )
            )
        )

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks some_bih for your time and effort to get this calculation correct. I am sure the above calculation will give the desired output.

      What I discovered later is that I was making use of column 'Hours of Operational Use' from wrong table which was causing this problem. After changing the table, the same DAX started giving the correct output. 

      Thanks Again.