You are on page 1of 7

1.

Objective of this Lesson#8:

 Understanding Relationships
 M-Language vs DAX & DAX
Introduction
 Working with Calculated Columns &
Measures

2. Query Editor vs Data Model

Calculations can be applied in both the Query Editor and


the Data Model
Exercise 1:

Create a Relationship Between Dim-Age and


Facts(Population) Table

Create a Report using Population versus Year as Stacked


column chart for Facts(Population) Table with legend as age-
category from Dim-Age Table
Exercise 2: Create the Relation Between Facts(population )
Table and Dim-Region Table and also Dim-gender Table

And create the chart as page 2


Exercise 3: Combine the Columns age-group and age-
category using M-Lang Power-Query as under:

let
Source = Table.Combine({#"population-2010-2019", #"population-2020-2029"}),
#"Appended Query" = Table.Combine({Source, #"population-2030-2040"}),
#"Filtered Rows" = Table.SelectRows(#"Appended Query", each true),
#"Removed Columns" = Table.RemoveColumns(#"Filtered
Rows",{"AgeGrpStart"}),
#"Removed Other Columns" = Table.SelectColumns(#"Removed
Columns",{"LocID", "Location", "Time", "AgeGrp", "PopMale", "PopFemale", "PopTotal"}),
#"Removed Columns1" = Table.RemoveColumns(#"Removed Other
Columns",{"PopTotal"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed
Columns1",{{"LocID", "country-id"}, {"Location", "country"}, {"Time", "year"}, {"AgeGrp",
"age-group"}, {"PopMale", "population-male"}, {"PopFemale", "population-female"}}),
#"Replaced Value" = Table.ReplaceValue(#"Renamed Columns","05. Sep","5-
9",Replacer.ReplaceText,{"age-group"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","Okt 14","10-
14",Replacer.ReplaceText,{"age-group"}),
#"Filtered Rows1" = Table.SelectRows(#"Replaced Value1", each true),
#"Replaced Value2" = Table.ReplaceValue(#"Filtered
Rows1","ERROR_6.246","6.246",Replacer.ReplaceText,{"population-male"}),
#"Changed Type" = Table.TransformColumnTypes(#"Replaced
Value2",{{"population-male", type number}, {"population-female", type number}}),
#"Rounded Off" = Table.TransformColumns(#"Changed Type",{{"population-
male", each Number.Round(_, 1), type number}, {"population-female", each
Number.Round(_, 1), type number}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Rounded Off",{{"country-
id", Int64.Type}, {"country", type text}, {"year", Int64.Type}, {"age-group", type text}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type1",
{"country-id", "country", "year", "age-group"}, "Attribute", "Value"),
#"Renamed Columns1" = Table.RenameColumns(#"Unpivoted
Columns",{{"Attribute", "gender"}, {"Value", "population"}}),
#"Sorted Rows" = Table.Sort(#"Renamed Columns1",{{"country",
Order.Ascending}, {"year", Order.Ascending}}),
#"Duplicated Column" = Table.DuplicateColumn(#"Sorted Rows", "age-group",
"age-group - Copy"),
#"Renamed Columns2" = Table.RenameColumns(#"Duplicated Column",{{"age-
group - Copy", "age-group-min"}}),
#"Extracted Text Before Delimiter" = Table.TransformColumns(#"Renamed
Columns2", {{"age-group-min", each Text.BeforeDelimiter(_, "-"), type text}}),
#"Replaced Value3" = Table.ReplaceValue(#"Extracted Text Before
Delimiter","100+","100",Replacer.ReplaceText,{"age-group-min"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Replaced Value3",{{"age-
group-min", Int64.Type}}),
#"Reordered Columns" = Table.ReorderColumns(#"Changed Type2",{"country-
id", "country", "year", "age-group-min", "gender", "population"}),
#"Sorted Rows1" = Table.Sort(#"Reordered Columns",{{"age-group-min",
Order.Ascending}, {"year", Order.Ascending}, {"country", Order.Ascending}}),
#"Extracted Text After Delimiter" = Table.TransformColumns(#"Sorted Rows1",
{{"gender", each Text.AfterDelimiter(_, "-"), type text}}),
#"Removed Other Columns1" = Table.SelectColumns(#"Extracted Text After
Delimiter",{"age-group"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Other Columns1"),
#"Duplicated Column1" = Table.DuplicateColumn(#"Removed Duplicates", "age-
group", "age-group - Copy"),
#"Renamed Columns3" = Table.RenameColumns(#"Duplicated Column1",{{"age-
group - Copy", "age-max"}}),
#"Replaced Value4" = Table.ReplaceValue(#"Renamed Columns3","100+","-
100",Replacer.ReplaceText,{"age-max"}),
#"Extracted Text After Delimiter1" = Table.TransformColumns(#"Replaced
Value4", {{"age-max", each Text.AfterDelimiter(_, "-"), type text}}),
#"Changed Type3" = Table.TransformColumnTypes(#"Extracted Text After
Delimiter1",{{"age-max", Int64.Type}}),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type3", "age-
category", each if [#"age-max"] <= 4 then "Baby" else if [#"age-max"] <= 14 then "Child"
else if [#"age-max"] <= 24 then "Teenager" else if [#"age-max"] <= 34 then "Young Adult"
else if [#"age-max"] <= 64 then "Adult" else "Senior"),
#"Changed Type4" = Table.TransformColumnTypes(#"Added Conditional
Column",{{"age-category", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type4", "Index", 1, 1,
Int64.Type),
#"Reordered Columns1" = Table.ReorderColumns(#"Added Index",{"Index",
"age-group", "age-max", "age-category"}),
#"Renamed Columns4" = Table.RenameColumns(#"Reordered
Columns1",{{"Index", "age-id"}}),
#"Duplicated Column2" = Table.DuplicateColumn(#"Renamed Columns4", "age-
group", "age-group - Copy"),
#"Reordered Columns2" = Table.ReorderColumns(#"Duplicated Column2",{"age-
id", "age-group", "age-group - Copy", "age-max", "age-category"}),
#"Renamed Columns5" = Table.RenameColumns(#"Reordered
Columns2",{{"age-group - Copy", "age-min"}}),
#"Replaced Value5" = Table.ReplaceValue(#"Renamed Columns5","100+","100-
",Replacer.ReplaceText,{"age-min"}),
#"Extracted Text Before Delimiter1" = Table.TransformColumns(#"Replaced
Value5", {{"age-min", each Text.BeforeDelimiter(_, "-"), type text}}),
#"Changed Type5" = Table.TransformColumnTypes(#"Extracted Text Before
Delimiter1",{{"age-min", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type5", "m-age-combined",
each [#"age-group"]&" "&[#"age-category"])
in
#"Added Custom"
Exercise 4: Combine the Columns age-group and age-
category using Dax Function CONCATENTE as under:

dax-age-combined = CONCATENATE([age-group],CONCATENATE(" ",[age-


category]))

Exercise 5: Create the following Measures in the population


Table

 Total Population

total-population = SUM([population])

 Average Population
average-population = AVERAGE([population])
and display the same using card chart type as page 3

Exercise 6: Create the following Measures in the population


Table and Female-Share in Dim-gender

 Female-Population

female-population = CALCULATE(SUM('FACT-population'[population]),'DIM-
gender'[gender]="Female",'FACT-population'[year]=2020)

 Female-Population-Filter
 female-population-filter = CALCULATE(SUM('FACT-population'[population]), FILTER('FACT-
population',[gender-id]=2 && [year]=2020))

 Female-Share in Dim-Gender

female-share = 'FACT-population'[female-population]/'FACT-population'[total-population]

You might also like