-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathXMLA - Partitions - Create.ps1
142 lines (123 loc) · 5.85 KB
/
XMLA - Partitions - Create.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
param (
$serverName = "powerbi://api.powerbi.com/v1.0/myorg/Session%20-%20PBI%20on%20Steroids"
, $databaseName = "WWI - Sales (Partitioned)"
, $years = (2013..2016)
)
$currentPath = (Split-Path $MyInvocation.MyCommand.Definition -Parent)
Import-Module "$currentPath\TOMHelper.psm1" -Force
$partitions = @(
@{
TableName = "Sales"
;
Partitions = @($years |% {
$year = $_
foreach($month in (1..12))
{
$partitionName = "Sales_$($year)_$($month.ToString().PadLeft(2,"0"))"
@{
Name = $partitionName
;
Type = "M"
;
Query = "let
Source = Sql.Database(Server, Database, [Query=""
SELECT
[Sale Key]
,[City Key]
,[Customer Key]
,[Bill To Customer Key]
,[Stock Item Key]
,[Invoice Date Key]
, case when (ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 10) > 5 then
DATEADD(DAY, ABS(CAST(CAST(NEWID() AS VARBINARY) AS INT)) % 300, [Invoice Date Key]) else
null end
[Invoice Paid Date Key]
,[Delivery Date Key]
, e.[WWI Employee ID] [Employee Key]
,[Quantity]
,[Unit Price]
,[Total Excluding Tax] [Total Amount]
,[Tax Amount]
,[Profit]
FROM [Fact].[Sale] s
left join [Dimension].[Employee] e on e.[Employee Key] = s.[Salesperson Key]
WHERE year([Invoice Date Key]) = $year and month([Invoice Date Key]) = $month""])
in
Source"
}
}
})
}
,
@{
TableName = "Orders"
;
Partitions = @($years |% {
$year = $_
$partitionName = "Orders_$($year)"
@{
Name = $partitionName
;
Type = "M"
;
Query = "let
Source = Sql.Database (Server, Database),
Table = Source{[Schema=""Fact"",Item=""Order""]}[Data],
#""Filtered Rows"" = Table.SelectRows(Table, each Date.Year([Order Date Key]) = $year),
KeepColumns = Table.SelectColumns(#""Filtered Rows"",{""Order Date Key"", ""City Key"", ""Customer Key"", ""Stock Item Key"", ""Salesperson Key"", ""Quantity"", ""Unit Price"", ""Tax Rate"", ""Total Excluding Tax"", ""Tax Amount"", ""Total Including Tax""})
in
KeepColumns"
}
})
}
,
@{
TableName = "Transactions"
;
Partitions = @($years |% {
$year = $_
$partitionName = "Transactions_$($year)"
@{
Name = $partitionName
;
Type = "M"
;
Query = "let
Source = Sql.Database(Server, Database),
Table = Source{[Schema=""Fact"",Item=""Transaction""]}[Data],
#""Expanded Dimension.Payment Method"" = Table.ExpandRecordColumn(Table, ""Dimension.Payment Method"", {""Payment Method""}, {""Dimension.Payment Method.Payment Method""}),
#""Filtered Rows"" = Table.SelectRows(#""Expanded Dimension.Payment Method"", each Date.Year([Date Key]) = $year),
KeepColumns = Table.SelectColumns(#""Filtered Rows"",{""Date Key"", ""Dimension.Payment Method.Payment Method"", ""Customer Key"", ""Bill To Customer Key"", ""Supplier Key"", ""Transaction Type Key"", ""Payment Method Key"", ""Total Excluding Tax"", ""Total Including Tax"", ""Is Finalized""}),
#""Changed Type"" = Table.TransformColumnTypes(KeepColumns,{{""Total Including Tax"", Currency.Type}, {""Total Excluding Tax"", Currency.Type}}),
#""Renamed Columns"" = Table.RenameColumns(#""Changed Type"",{{""Dimension.Payment Method.Payment Method"", ""Payment Method""}})
in
#""Renamed Columns"""
}
})
}
,
@{
TableName = "Purchases"
;
Partitions = @($years |% {
$year = $_
$partitionName = "Purchases_$($year)"
@{
Name = $partitionName
;
Type = "M"
;
Query = "let
Source = Sql.Database(Server, Database),
Fact_Purchase = Source{[Schema=""Fact"",Item=""Purchase""]}[Data],
#""Filtered Rows"" = Table.SelectRows(Fact_Purchase, each Date.Year([Date Key]) = $year),
#""Removed Other Columns"" = Table.SelectColumns(#""Filtered Rows"",{""Purchase Key"", ""Date Key"", ""Supplier Key"", ""Stock Item Key"", ""Ordered Quantity"", ""Is Order Finalized""}),
#""Renamed Columns"" = Table.RenameColumns(#""Removed Other Columns"",{{""Date Key"", ""Purchase Date Key""}})
in
#""Renamed Columns"""
}
})
}
)
$results = Add-ASTablePartition -serverName $serverName -databaseName $databaseName -partitions $partitions -removeDefaultPartition -Verbose
$results