Forum Discussion
Issue with Averaging Values Across Filtered Descriptions After Table Merge
Hello Power BI Community,
I'm encountering a perplexing issue with DAX in Power BI Desktop and could use your collective expertise. My goal is to calculate the average of a column ('DiferençaHorimetro') for each unique description in another column ('Descrição Completa'), regardless of other factors such as material or order number. However, despite applying a filter to only consider a specific description, I'm getting multiple average values within the 'MediaTrocaCol' column for the same description.
Here's a summary of the situation:
- I have a table with columns: MATERIAL, ORDEM, Descrição Completa, MediaTrocaCol, and DiferençaHorimetro.
- The 'Descrição Completa' column is a result of merging data from another query.
- I applied a filter in Power BI to focus on a specific description to test the average calculation.
- The formula in DAX aims to calculate the average 'DiferençaHorimetro' for each 'Descrição Completa' while excluding zeros.
- After trying several formula adjustments, including the use of ALLEXCEPT and REMOVEFILTERS, I still end up with multiple averages for the same description.
Here's the latest version of the DAX formula I'm using:
MediaTrocaCol =
VAR DescricaoAtual = TRIM(UPPER('SAP - MB51'[Descrição Completa]))
VAR HorasOperadas = 'SAP - MB51'[DiferençaHorimetro]
VAR MediaDiferenca =
CALCULATE(
AVERAGE('SAP - MB51'[DiferençaHorimetro]),
REMOVEFILTERS('SAP - MB51'),
TRIM(UPPER('SAP - MB51'[Descrição Completa])) = DescricaoAtual,
'SAP - MB51'[DiferençaHorimetro] > 0
)
RETURN
IF(
ISBLANK(HorasOperadas),
BLANK(),
MediaDiferenca
)
Despite the formula and the filter, I'm still seeing various average values in the 'MediaTrocaCol' for what should be the same 'Descrição Completa'. A single average value is expected for each unique description.
I've attached a screenshot for reference.
Does anyone have insights into what might be causing this issue or suggestions on how to resolve it? Any help would be greatly appreciated!
Best regards, Bruno M.
2 Replies
- lbendlinSuper User
Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information or anything not related to the issue or question.
If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216
Please show the expected outcome based on the sample data you provided.
Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523- BrunoMFerreiraNew Member
Here's a breakdown of all the data required to grasp the context. I am attempting to calculate the average using a calculated column in [MediaTrocaCol], but it results in multiple averages for the same [Complete Description]. After filtering out zeros, the average should have been 743.77 for all descriptions, I was only able to include 10 lines. I am also including all calculated columns relevant to this calculation.
[DiferençaHorimetro] represents the difference between the last and the penultimate change of the same description [Complete Description] for the same equipment (same [TAG]). The DAX formula is as follows:
```
DiferençaHorimetro = IF(ISBLANK('SAP - MB51'[HorimetroTrocaPenultima]),0,
'SAP - MB51'[HorimetroTrocaUltima] - 'SAP - MB51'[HorimetroTrocaPenultima])
```[HorimetroTrocaPenultima] is the hour meter reading for the penultimate change of the equipment [TAG]. The DAX formula is:
```
HorimetroTrocaPenultima =VAR Description = 'SAP - MB51'[Complete Description]
VAR Equipment = 'SAP - MB51'[TAG]
VAR LastPurchaseDate = 'SAP - MB51'[PreviousChangeDate]RETURN CALCULATE(
MAX('SAP - IK17'[MEASURED_VALUE]), 'SAP - IK17'[EQUIPMENT] = Equipment, 'SAP - IK17'[NOMINATION]="HOUR METER", 'SAP - IK17'[CREATION_DATE] <= LastPurchaseDate)
```[HorimetroTrocaUltima] is the hour meter reading for the latest change of the equipment [TAG]. The DAX is:
```
HorimetroTrocaUltima =VAR Description = 'SAP - MB51'[Complete Description]
VAR Equipment = 'SAP - MB51'[TAG]
VAR LastPurchaseDate = 'SAP - MB51'[ENTRY_DATE]RETURN CALCULATE(
MAX('SAP - IK17'[MEASURED_VALUE]), 'SAP - IK17'[EQUIPMENT] = Equipment, 'SAP - IK17'[NOMINATION]="HOUR METER", 'SAP - IK17'[CREATION_DATE] <= LastPurchaseDate)
```[PreviousChangeDate] is the valid date (excluding chargebacks) when the item [Complete Description] was last changed. The DAX is:
```
PreviousChangeDate =VAR Description = 'SAP - MB51'[Complete Description]
VAR Equipment = 'SAP - MB51'[TAG]
VAR CurrentDate = 'SAP - MB51'[ENTRY_DATE]
VAR FilteredTable =
FILTER(
ALL('SAP - MB51'),
'SAP - MB51'[TAG] = Equipment &&
'SAP - MB51'[Complete Description] = Description &&
'SAP - MB51'[ENTRY_DATE] < CurrentDate
)VAR PenultimateDate = MAXX(FilteredTable, 'SAP - MB51'[ENTRY_DATE])
RETURN PenultimateDate
``TAG DATA_ENTRADA Ordem Descrição Completa DiferençaHorimetro HorimetroTrocaPenultima HorimetroTrocaUltima DataTrocaAnterior MediaTrocaCol CR-3014 14/02/2023 00:00 4447690 INJETOR 12V4000 EX59407500020 MTU 0 13/01/2023 00:00 2553 EH-5033 17/08/2023 00:00 4520457 INJETOR 12V4000 EX59407500020 MTU 76 196164 196240 16/08/2023 00:00 2553 EH-5005 18/12/2023 00:00 4534546 INJETOR 12V4000 EX59407500020 MTU 212 202908 203120 29/08/2022 00:00 2553 CR-4017 27/06/2022 00:00 4413230 INJETOR 12V4000 EX59407500020 MTU 0 301269 1915 EH-5022 24/10/2022 00:00 4442519 INJETOR 12V4000 EX59407500020 MTU 0 158661 158661 12/07/2022 00:00 1915 EH-5006 23/11/2022 00:00 4448702 INJETOR 12V4000 EX59407500020 MTU 90 174615 174705 21/11/2022 00:00 1915 CR-4014 08/07/2023 00:00 4509081 INJETOR 12V4000 EX59407500020 MTU 2343 32889 35232 10/05/2023 00:00 1915 14/06/2022 00:00 501982 INJETOR 12V4000 EX59407500020 MTU 0 26/04/2022 00:00 1276 09/01/2023 00:00 501982 INJETOR 12V4000 EX59407500020 MTU 0 02/12/2022 00:00 1276 CR-4001 21/01/2022 00:00 4373769 INJETOR 12V4000 EX59407500020 MTU 0 136680 1276 CR-3058 21/02/2022 00:00 4379342 INJETOR 12V4000 EX59407500020 MTU 0 107836 1276 EH-6005 13/04/2022 00:00 4393294 INJETOR 12V4000 EX59407500020 MTU 0 187274 187274 09/04/2022 00:00 1276 EQNCUMYA 25/09/2022 00:00 4407050 INJETOR 12V4000 EX59407500020 MTU 0 06/06/2022 00:00 1276 EH-5022 12/07/2022 00:00 4416705 INJETOR 12V4000 EX59407500020 MTU 0 105774 1276 CR-4023 21/09/2022 00:00 4435130 INJETOR 12V4000 EX59407500020 MTU 0 166864 1276 CR-3087 11/10/2022 00:00 4439264 INJETOR 12V4000 EX59407500020 MTU 0 167798 1276 EH-5006 13/10/2022 00:00 4440280 INJETOR 12V4000 EX59407500020 MTU 3238 112084 115322 23/06/2022 00:00 1276 CR-4002 24/10/2022 00:00 4442463 INJETOR 12V4000 EX59407500020 MTU 4318 76558 80876 13/05/2022 00:00 1276 EH-5006 23/11/2022 00:00 4442649 INJETOR 12V4000 EX59407500020 MTU 60 116410 116470 21/11/2022 00:00 1276 CR-3075 15/11/2022 00:00 4447322 INJETOR 12V4000 EX59407500020 MTU 0 117960 117960 30/08/2022 00:00 1276 EH-5002 28/11/2022 00:00 4450092 INJETOR 12V4000 EX59407500020 MTU 0 114568 114568 23/11/2022 00:00 1276 EH-5027 31/03/2023 00:00 4478090 INJETOR 12V4000 EX59407500020 MTU 66 43872 43938 28/03/2023 00:00 1276 EH-5020 03/04/2023 00:00 4479397 INJETOR 12V4000 EX59407500020 MTU 2302 114582 116884 28/10/2022 00:00 1276 EH-6003 08/04/2023 00:00 4481278 INJETOR 12V4000 EX59407500020 MTU 0 147966 147966 05/04/2023 00:00 1276 CR-3064 06/05/2023 00:00 4491220 INJETOR 12V4000 EX59407500020 MTU 1094 148772 149866 24/03/2023 00:00 1276 EH-5024 09/06/2023 00:00 4501207 INJETOR 12V4000 EX59407500020 MTU 104 29356 29460 06/06/2023 00:00 1276 CB-3069 28/07/2023 00:00 4514790 INJETOR 12V4000 EX59407500020 MTU 0 52484 1276 EH-5016 16/08/2023 00:00 4518317 INJETOR 12V4000 EX59407500020 MTU 0 83212 1276 EH-5033 14/08/2023 00:00 4519574 INJETOR 12V4000 EX59407500020 MTU 122 97796 97918 09/08/2023 00:00 1276 EH-6005 18/08/2023 00:00 4520965 INJETOR 12V4000 EX59407500020 MTU 0 187274 187274 30/06/2023 00:00 1276 EH-5006 19/08/2023 00:00 4521217 INJETOR 12V4000 EX59407500020 MTU 0 118952 118952 14/06/2023 00:00 1276 CR-3038 01/09/2023 00:00 4525507 INJETOR 12V4000 EX59407500020 MTU 0 96850 1276 CR-3038 02/09/2023 00:00 4525507 INJETOR 12V4000 EX59407500020 MTU 0 96850 96850 01/09/2023 00:00 1276 EH-5024 18/12/2023 00:00 4534564 INJETOR 12V4000 EX59407500020 MTU 1086 33032 34118 09/11/2023 00:00 1276 EH-5033 13/10/2023 00:00 4537877 INJETOR 12V4000 EX59407500020 MTU 522 98942 99464 20/09/2023 00:00 1276 EH-5010 21/11/2023 00:00 4548473 INJETOR 12V4000 EX59407500020 MTU 90 104508 104598 18/11/2023 00:00 1276 EH-5017 05/12/2023 00:00 4552710 INJETOR 12V4000 EX59407500020 MTU 96 81032 81128 03/12/2023 00:00 1276 CR-3099 27/12/2023 00:00 4558509 INJETOR 12V4000 EX59407500020 MTU 0 220408 1276