## Data Transformation with Power BI and DAX

##### Description

Through this project, we aim to seamlessly integrate and correct mismatches from two tables - Audit and TableCR. The project revolves around refining the 'Rating' column in the Audit table by comparing it with the 'RatingCheck' column in the TableCR table using the logic in DAX. If there is a mismatch, the corresponding value in the 'Rating' column gets replaced by a default value, '3-FAIR', whilst retaining the matching ones. The primary challenge is to tweak the initial DAX code to avoid replacing every single value with the default one.

# Understanding Power BI & DAX for Data Transformation

In this section, we're going to walk through the practicalities of using DAX functions to manipulate data within Power BI. DAX stands for Data Analysis Expressions, a library of functions used in Power BI, Analysis Services, and Power Pivot in Excel for data modeling and arithmetic calculations.

## Set-Up Instructions

Before diving into DAX in Power BI, make sure you have Power BI Desktop installed. Once installed, you can import or connect to your dataset. For simplicity, we will use a sample dataset 'Sales' with columns 'Salesperson, 'Region', 'Month', 'Year' and 'SalesAmount'.

## Loading Data into Power BI

First, we load our sample data into Power BI. Use **Home > Get Data > Excel/csv/etc.**, depending on your data source. Select the data file and choose 'Sales' from the queried data. Press 'Load', and your dataset should appear in Power BI.

Now that the data is loaded, we can start using DAX to refine and correct mismatches.

## 1. Create Calculated Columns with DAX

We can use DAX to create new calculated columns in the data model.

Suppose we need to calculate the annual sales, we can create a new column 'AnnualSales' that sums up the 'SalesAmount' for each 'Year'.

Go to **Modeling > New Column**, enter:

`AnnualSales = CALCULATE(SUM('Sales'[SalesAmount]), ALLEXCEPT('Sales', 'Sales'[Year]))`

This will create a new column 'AnnualSales' with the calculated annual sales for each year.

## 2. Correcting Data Mismatches using DAX

DAX can be useful for rectifying data mismatches. For example, let's assume our sales amount figures have some entries registered as negative values, which is a data mismatch.

To correct this, we can write a new DAX expression that makes the negative values positive:

In the 'Sales' table, go to **Modeling > New Column**, enter:

```
CorrectedSalesAmount =
IF ('Sales'[SalesAmount] < 0,
ABS('Sales'[SalesAmount]),
'Sales'[SalesAmount]
)
```

This DAX formula checks if 'SalesAmount' is negative, if so it turns it to a positive value using the ABS function, otherwise leaves it as is.

## 3. Refining Data with DAX

We can use DAX to refine our data. Let’s assume the 'Region' column has some mismatches such as the same region being called by two different names.

To correct this, we can write a DAX expression to standardize the 'Region' names:

In the 'Sales' table, go to **Modeling > New Column**, enter:

```
RefinedRegion =
SWITCH (
'Sales'[Region],
"N. America", "North America",
"S. America", "South America",
'Sales'[Region] -- Default
)
```

The SWITCH function in DAX checks each row in the 'Region' column, and if it says "N. America", it changes it to "North America", if it says "S. America", it changes it to "South America". If the region is not one of these two, it leaves it as is.

That's it! We have used DAX in Power BI to transform, correct mismatches, and refine data.

## Data Transformation Techniques Using DAX in Power BI

Data transformation is a critical part of any data analysis project. When it's performed right, data transformation can enhance the precision of analysis and consequently the insights drawn from the data. Before jumping into the practical implementation, let's briefly mention that Data Analysis Expressions (DAX) is a formula language for Power BI, Analysis Services, and Power Pivot in Excel.

Transforming data means modifying it from its original form into a format that is more appropriate for your specific purposes. This can involve a plethora of activities, such as normalizing data, dealing with missing data, or correcting mistakes and discrepancies. In DAX, we have several functions that help us achieve these tasks.

### 1. Dealing with missing data

When dealing with missing data, we typically want to either fill in the missing values, or filter them out completely.

Example of missing value replacement:

```
EVALUATE
SUMMARIZE (
ADDCOLUMNS (
'Table',
"ModifiedColumn", IF ( ISBLANK ( 'Table'[Column] ), 0, 'Table'[Column] )
),
[ModifiedColumn]
)
```

In this example, we add a new column `ModifiedColumn`

to `Table`

. If the value in `Column`

is blank (missing), we replace it with 0. Otherwise, we just keep the original value.

### 2. Normalizing data

When normalizing data, you typically want to bring all of your data within a particular range. This could be between 0 and 1, -1 and 1, or any other range.

Example of Min-Max normalization:

```
EVALUATE
SUMMARIZE (
ADDCOLUMNS (
'Table',
"NormalizedColumn", ( 'Table'[Column] - MIN ( 'Table'[Column] ) ) / ( MAX ( 'Table'[Column] ) - MIN ( 'Table'[Column] ) )
),
[NormalizedColumn]
)
```

Here, each value in `Column`

is replaced with a new value that accounts for the range of data in `Column`

.

### 3. Correcting data discrepancies

Using IF statement, we can correct data discrepancies. For example, if we know that certain values in a column are erroneous, we can correct them.

Example of correcting data:

```
EVALUATE
SUMMARIZE (
ADDCOLUMNS (
'Table',
"CorrectedColumn", IF ( 'Table'[Column] = "WrongValue", "CorrectValue", 'Table'[Column] )
),
[CorrectedColumn]
)
```

In this case, we replace the values "WrongValue" in `Column`

with "CorrectValue". Any other value is left unchanged.

### In Conclusion

These are just a few examples of the most common data transformation tasks you might have to perform in Power BI using DAX. Each of these snippets can be applied to any table in your Power BI project simply by replacing 'Table' and 'Column' with your actual table and column names. These basic concepts can also be expanded upon and combined to perform more complex transformations as needed.

# Familiarizing with Tabular Models and Audit Tables in Power BI using DAX

This guide will highlight how to get familiar with handling and manipulating Tabular Models (TableCR) and Audit Tables using DAX in Power BI.

## Working with Tables in DAX

Just like in any other query language, tables in DAX act as the foundational data structure wherein rows comprise individual data points and columns comprise the attributes of the data points.

### Creating a TableCR using DAX in Power BI

A TableCR (or Table Constructor) is used to create a table of values. To create a TableCR in Power BI, you’ll use the DATATABLE function. This function creates small tables in DAX using static data.

```
SalesTableCR =
DATATABLE (
"Product", STRING,
"Quantity", INTEGER,
{
{ "Product A", 15 },
{ "Product B", 30 }
}
)
```

In this example, "SalesTableCR" is the new TableCR created compromising two columns - Product and Quantity.

### Selecting Data from a TableCR using DAX in Power BI

The SELECTCOLUMNS function can be used to select data from a TableCR. It provides a way to select a subset of columns and express new columns so that the result of the function is a new table that includes the selected and the new columns only.

Syntax:

`SELECTCOLUMNS( `, "NewColumnName1", [, "NewColumnName2", [, ...] ])

### Modifying a TableCR using DAX in Power BI

Use ADDCOLUMNS function to extend a table by adding new columns, calculated based on the existing data or from the expressions provided.

Syntax:

`ADDCOLUMNS ( `, "NewColumnName", [, "NewColumnName", [, ...] ])

## Working with Audit Tables in Power BI using DAX

Audit tables are essentially system-generated logs that capture details about data manipulation and transaction operations. These typically include details such as the operation performed, the user who performed the operation, and the timestamp of the operation.

### Creating Audit Tables using DAX in Power BI

In Power BI, creating an audit table would entail pulling data from an existing source table and manipulating it into the audit format. For example, consider a simplistic audit log table "AuditLog" comprising columns - UserName, Action, and TimeStamp.

```
AuditLog =
ADDCOLUMNS (
"UserName", User[UserName],
"Action", User[Action],
"Timestamp", NOW()
)
```

Here, the ADDCOLUMNS function is used to add three new columns to the "AuditLog" table.

### Modifying Audit Tables using DAX in Power BI

Like any other table, you can use UPDATE(), ADDCOLUMNS(), or SELECTCOLUMNS() functions to update, add, or select specific columns from the audit tables.

In conclusion, the essence of becoming familiar with handling Tabular Models and Audit Tables in Power BI using DAX is practice and hands-on experimentation. Remember to always verify your data and formulas for accuracy to ensure validity in your reports.

# Mastering DAX for Data Correction

This document focuses on refining and correcting data mismatches in Power BI using Data Analysis Expressions (DAX).

## Data Correction Strategies in DAX

There are different methods that are typically adopted to perform data correction in DAX. These are:

- Error Handling Expressions - Using
`TRY/CATCH`

methods to manage errors. - Conditional Statements - Using functions
`If()`

,`Switch()`

. - Lookup Techniques - Using functions like
`LOOKUPVALUE()`

. - Date and Time Functions - Using functions such as
`DAY()`

,`MONTH()`

,`YEAR()`

,`TODAY()`

,`NOW()`

to handle date and time data.

You can use a combination of these methods to handle your specific data correction requirements.

## Error Handling Expressions

DAX offers `TRY/CATCH`

functionality to handle errors in your data. The `TRY()`

function attempts to calculate its first argument and return that value. If an error occurs, the function returns its second argument.

Example:

Result = TRY([Calculation], "Error in Calculation")

In the above expression, if `[Calculation]`

results in an error, then `Result`

will be `"Error in Calculation"`

.

## Conditional Statements

The `IF()`

and `SWITCH()`

functions are primarily used to handle conditional computations in Power BI.

Example:

NewColumn = IF(ISERROR([ExistingColumn]), BLANK(), [ExistingColumn])

The `SWITCH()`

function is used when we have more than two conditions to check.

Example:

NewColumn = SWITCH ( TRUE(), ISBLANK([ExistingColumn]), "No Value", [ExistingColumn] < 0, "Negative Value", [ExistingColumn] > 0, "Positive Value", "Zero Value" )

## Lookup Techniques

The `LOOKUPVALUE()`

function retrieves the value of a column from another table given a related value. If any inconsistencies are detected, you can use `LOOKUPVALUE()`

to correct the values according to another table.

Example:

NewColumn = LOOKUPVALUE( 'LookupTable'[LookupColumn], 'LookupTable'[RelatedColumn], 'DataTable'[DataColumn], "FallbackValue" )

## Date and Time Functions

Date and time functions are useful when working with temporal data. If the data contains incorrect or missing values in date or time columns, you can use the relevant DAX functions to correct these.

Example:

NewDate = IF( ISERROR(DATE([Year], [Month], [Day])), TODAY(), DATE([Year], [Month], [Day]) )

With the above DAX expressions, you should be able to handle most types of data discrepancies that typically occur in the context of Power BI data models. The examples provided are simplistic in nature and you may need to adapt them according to your data correction needs.

Remember, the key idea in any data correction procedure is to ensure that the governance and quality of your data is maintained without distorting the underlying information. Therefore, be sure to test your functions thoroughly and review the results periodically.

Good luck with data cleaning!

# Implementing Default Values for Non-Matching Entries with DAX

This section details the DAX operations required to implement default values for non-matching entries in Power BI.

## Categories of Mismatches

Before incorporating the default value functionality, it's pivotal to understand the type of mismatches we might encounter in our dataset:

**Missing Values:**This happens when we have blank spaces or`NULL`

instances in our data.**Inconsistent Values:**When we have matching values represented differently. E.g., "Data Scientist" and "data scientist".

## Implementing Default Values

### 1. Handling Missing Values

To replace missing values, we use the `COALESCE`

function in DAX. `COALESCE`

returns the first argument that does not evaluate to a blank or NULL. If all arguments evaluate to blank or NULL, `COALESCE`

returns blank.

The basic syntax of `COALESCE`

is as follows:

`COALESCE(`, , ...)

Here's an example implementation for a column `ColumnName`

of table `TableName`

, where we are replacing the missing values with 'Default':

`TableName[ColumnName] = COALESCE(TableName[ColumnName], "Default")`

### 2. Addressing Inconsistent Values

For inconsistent values, creating a new calculated column that standardizes the values can be useful.

Suppose `ColumnName`

is the column in `TableName`

with inconsistent entries. We can correct it as follows:

`TableName[StandardisedColumnName] = TRIM(UPPER(TableName[ColumnName]))`

Here, `TRIM`

removes extra spaces and `UPPER`

converts all the data to upper case to maintain consistency.

## Applying Default Value for Non-Matching Entries

Let's say we have two tables - `MainTable`

and `LookupTable`

. We want to replace non-matching entries in the `MainTable`

's column `ColumnName`

with 'Default'.

Before we can perform this operation, it's mandatory to carry out a `LEFT OUTER JOIN`

operation between `MainTable`

and `LookupTable`

. This operation is essential to identify non-matching entries.

Implement this using the `MERGE`

function as shown:

`MergedTable = MERGE(MainTable, LookupTable, LEFT JOIN, ON MainTable.ColumnName = LookupTable.ColumnName)`

After the `MERGE`

operation, non-matching values from the `MainTable`

get a NULL value. Assign them a default value:

`MergedTable[ColumnName] = COALESCE(MergedTable[ColumnName], "Default")`

In the end, don't forget to hide the original column `ColumnName`

from `MainTable`

in the report view, so that only the new column with the corrected data is visible for report making.

Please remember that these data transformations do not affect the original data source, as Power BI operates using a read-only connection to your data. The changes made are only available within the current Power BI desktop file.

In this way, you can handle default values for non-matching entries in Power BI using DAX.