You are learning Power Query in MS Excel
How to use aggregation functions like MIN, MAX, and VAR in Power Query?
In Power Query, you can use aggregation functions like MIN, MAX, and VAR to compute summary statistics or perform aggregations on data columns. These functions are useful for summarizing data within groups or across entire datasets. Here’s how you can use them in Power Query:
Using Aggregation Functions in Power Query
1. Open Power Query Editor
- Launch Power BI Desktop or Excel.
- Load your data into Power Query: `Home` > `Get Data` > select your data source > `Transform Data`.
2. Applying Aggregation Functions
- Select the Column: Choose the column on which you want to apply the aggregation function.
- Access Transformations:
- Click on the column header to select it.
- Go to `Transform` tab in the Power Query Editor.
- Apply Aggregation:
- MIN (Minimum Value):
- To find the minimum value in a column, use the `List.Min` function:
```m
List.Min(Table.ColumnName)
```
- Replace `ColumnName` with the actual name of your column.
- MAX (Maximum Value):
- To find the maximum value in a column, use the `List.Max` function:
```m
List.Max(Table.ColumnName)
```
- Replace `ColumnName` with the actual name of your column.
- VAR (Variance):
- To calculate the variance of values in a column, use the `List.Variance` function:
```m
List.Variance(Table.ColumnName)
```
- Replace `ColumnName` with the actual name of your column.
Example: Using Aggregation Functions
Suppose you have a dataset `SalesData` with columns `Product`, `Price`, and `Quantity`. To find the minimum and maximum prices, and the variance of quantities:
1. Open Power Query Editor:
- Select `Home` > `Transform Data` to open the Power Query Editor.
2. Apply Aggregation Functions:
- Minimum Price:
- Select the `Price` column.
- Go to `Transform` > `Aggregate` > `Minimum`.
- Power Query generates a step like:
```m
#"Minimum Price" = List.Min(SalesData[Price])
```
- Maximum Price:
- Select the `Price` column.
- Go to `Transform` > `Aggregate` > `Maximum`.
- Power Query generates a step like:
```m
#"Maximum Price" = List.Max(SalesData[Price])
```
- Variance of Quantity:
- Select the `Quantity` column.
- Go to `Transform` > `Aggregate` > `Variance`.
- Power Query generates a step like:
```m
#"Variance of Quantity" = List.Variance(SalesData[Quantity])
```
3. Load Data:
- After applying the aggregation functions, click `Close & Load` to load the summarized data back into Power BI or Excel.
Additional Tips:
- Grouping Data: Use `Group By` feature in Power Query to perform aggregations by groups based on one or more columns.
- Multiple Aggregations: Combine multiple aggregations in a single step by creating custom columns using M language functions (`List.Min`, `List.Max`, `List.Variance`, etc.).
- Error Handling: Ensure data types are compatible with aggregation functions to avoid errors, especially when dealing with mixed data types in columns.
By using these aggregation functions in Power Query, you can efficiently summarize and compute statistics from your data before further analysis or visualization in Power BI or Excel.