Skip to content

Instantly share code, notes, and snippets.

@ncalm
Created November 22, 2023 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncalm/e154131bac98e843c28d8820c0b894fa to your computer and use it in GitHub Desktop.
Save ncalm/e154131bac98e843c28d8820c0b894fa to your computer and use it in GitHub Desktop.
SQL view and Power Query for replicating GROUPBY testing workbook
/* Install Adventure Works DW 2019 from this link:
https://github.com/Microsoft/sql-server-samples/releases/download/adventureworks/AdventureWorksDW2019.bak
View below is used by Power Query in GROUPBY demonstrations
*/
USE [AdventureWorksDW2019]
GO
/****** Object: View [dbo].[vSales] Script Date: 11/22/2023 12:02:03 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[vSales]
AS
SELECT
fis.OrderDate,
pc.EnglishProductCategoryName AS category,
ps.EnglishProductSubcategoryName AS sub_category,
p.EnglishProductName AS product,
fis.SalesAmount,
fis.OrderQuantity
FROM FactInternetSales fis
INNER JOIN DimProduct p ON fis.ProductKey = p.ProductKey
INNER JOIN DimProductSubcategory ps ON p.ProductSubcategoryKey = ps.ProductSubcategoryKey
INNER JOIN DimProductCategory pc ON ps.ProductCategoryKey = pc.ProductCategoryKey;
GO
let
Source = Sql.Databases("YOURSERVERINSTANCE"),
AdventureWorksDW2019 = Source{[Name="AdventureWorksDW2019"]}[Data],
dbo_vSales = AdventureWorksDW2019{[Schema="dbo",Item="vSales"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(dbo_vSales,{{"OrderDate", type date}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"category", "Category"}, {"sub_category", "SubCategory"}, {"product", "Product"}})
in
#"Renamed Columns"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment