0% found this document useful (0 votes)
10 views7 pages

SQL Skills in Power BI

Uploaded by

Alferino Filho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

SQL Skills in Power BI

Uploaded by

Alferino Filho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL to DAX Query to DAX Measure #1

SQL and DAX serve similar purposes in querying and manipulating data, but they have different syntax
and concepts. Here are some equivalents between SQL and DAX. By identifying the key calculations and
ensuring the result is scalar, you can effectively convert DAX queries to measures.

SQL Query DAX Query DAX Measure


Query all rows and columns from a 1. Query all rows and columns from a A measure is a calculation used to
1.
table table aggregate data dynamically. Unlike
SQL and DAX queries, which can
SELECT * EVALUATE return multiple rows and columns, a
FROM FactSales; CALCULATETABLE ( FactSales ) measure provides a single scalar
value. Measures in Power BI require
an aggregation function, such as
Query only 100 rows from a table Query only 100 rows from a table SUM, AVERAGE, COUNT, or others, to
2. 2. perform their calculations and
SELECT * EVALUATE provide meaningful insights based
FROM FactSales TOPN ( 100, FactSales ) on the report’s context or
visualization.
LIMIT 100;

Sort the results by SalesKey in Sort the results by SalesKey in


3. 3. Ascending order
Ascending order
SELECT * EVALUATE
FROM FactSales CALCULATETABLE ( FactSales )
ORDER BY SalesKey ASC; ORDER BY FactSales[SalesKey] ASC

Query only a few columns from a Query only a few columns from a
4. table 4. table
SELECT EVALUATE
BrandName, SELECTCOLUMNS (
ProductName DimProduct,
FROM DimProduct; DimProduct[BrandName],
DimProduct[ProductName]
) #Bilal
#Bilal
SQL to DAX Query to DAX Measure #2

SQL Query DAX Query DAX Measure

5. Query unique rows from a table 5. Query unique rows from a table A measure is a calculation used to
aggregate data dynamically. Unlike
SELECT DISTINCT * EVALUATE
SQL and DAX queries, which can
FROM DimProduct; DISTINCT ( DimProduct ) return multiple rows and columns, a
measure provides a single scalar
value. Measures in Power BI require
6. Query unique rows from a column 6. Query unique rows from a column of an aggregation function, such as
of a table a table SUM, AVERAGE, COUNT, or others, to
perform their calculations and
SELECT EVALUATE
provide meaningful insights based
DISTINCT BrandName DISTINCT ( DimProduct[BrandName] ) on the report’s context or
visualization.
FROM DimProduct;
Query a table and filter row with
7.
a condition
Query a table and filter row with
7. a condition EVALUATE
SELECT * FILTER (
FROM DimProduct DimProduct,
WHERE DimProduct[BrandName] =
"Contoso"
BrandName = "Contoso";
)

8. Query a table and filter row with


a wildcard 8. Query a table and filter row with
a wildcard
SELECT *
EVALUATE
FROM DimProduct
FILTER (
WHERE BrandName LIKE "Con%";
DimProduct,
CONTAINSSTRING(
DimProduct[BrandName], "Con?" )
)
SQL to DAX Query to DAX Measure #3

SQL Query DAX Query DAX Measure


9. Query a table having missing 9. Query a table having missing
values values
EVALUATE
SELECT *
FILTER ( DimGeography,
FROM DimGeography
NOT ISBLANK (
WHERE CityName IS NOT NULL;
DimGeography[CityName] ))

Query a table with values from a


10. 10. Query a table with values from a
list
list
SELECT *
EVALUATE
FROM FactSales
FILTER ( FactSales,
WHERE SalesKey IN (100, 200,
FactSales[SalesKey]
300);
IN {100, 200, 300})

11. Count the number of rows in a


table
Count the number of rows in a
11. table
. Count the number of rows in a table

SELECT Total Rows Sales Table =


EVALUATE
COUNT(*) AS Total_Rows COUNTROWS ( FactSales )
ROW ( "Total_Rows", COUNTROWS (
FROM FactSales; FactSales ) )

Count the unique values in a Count the total values in a Count the total values in a column
12.
column
12.
column . Total_Brands =
SELECT EVALUATE DISTINCTCOUNT (
COUNT(DISTINCT BrandName) AS ROW ( "Total_Brands", DimProduct[BrandName] )
Total_Brands
DISTINCTCOUNT (
FROM DimProduct; DimProduct[BrandName] )
)
#Bilal
SQL to DAX Query to DAX Measure #4

SQL Query DAX Query DAX Measure

13. Aggregate values in a column with


a condition
13. Aggregate values in a column
with a condition
. Aggregate values in a column with a
condition
SELECT EVALUATE 2008_SalesAmount =
SUM(SalesAmount) AS ROW ( "2008_SalesAmount", CALCULATE (
2008_SalesAmount CALCULATE ( SUM ( FactSales[SalesAmount] ),
FROM FactSales SUM ( FactSales[SalesAmount] KEEPFILTERS ( YEAR (
WHERE YEAR(DateKey) = 2008; ), FactSales[DateKey] ) = 2008)
KEEPFILTERS( YEAR( )
FactSales[DateKey] ) = 2008 )
)
)
14. Cross-join multiple tables
SELECT
14. Cross-join multiple tables
g.CityName,
EVALUATE
st.SalesTerritoryCountry
CROSSJOIN (
FROM
SELECTCOLUMNS (
DimGeography g DimGeography,
CROSS JOIN DimSalesTerritory st; DimGeography[CityName] ),
SELECTCOLUMNS (
DimSalesTerritory,
DimSalesTerritory[SalesTerritory
Country] )
)

#Bilal
#Bilal
SQL to DAX Query to DAX Measure #5

SQL Query DAX Query DAX Measure


Query multiple tables with inner 15. Query multiple tables with inner join We can effectively convert a DAX query
15. into a measure by identifying the main
join EVALUATE calculation, ensuring the result is a
SELECT SELECTCOLUMNS ( DimProduct, scalar value, and using appropriate DAX
functions. This allows the measure to be
c.ProductCategoryName, "ProductCategoryName", RELATED ( dynamically calculated based on the
DimProductCategory[ProductCategoryName] report's filter context.
sc.ProductSubCategoryName, ),
p.ProductName The following is an example of
"ProductSubCategoryName", RELATED ( converting a DAX query into a DAX
FROM DimProduct p DimProductSubcategory[ProductSubcategor measure.
yName] ),
INNER JOIN DimProductSubcategory
DimProduct[ProductName]
sc ON p.ProductSubCategoryKey = Count Unique Products =
sc.ProductSubCategoryKey )
VAR _Product =
INNER JOIN DimProductCategory c
SELECTCOLUMNS (
ON sc.ProductCategoryKey = Query multiple tables with left join
c.ProductCategoryKey; 16. DimProduct,
EVALUATE
"ProductCategoryName", RELATED (
NATURALLEFTOUTERJOIN ( DimProductCategory[ProductCategoryName]
16. Query multiple tables with left ),
SELECTCOLUMNS ( DimGeography,
join
"GeographyKey", CONVERT ( "ProductSubCategoryName",
SELECT DimGeography[GeographyKey], INTEGER ), RELATED (
DimProductSubcategory[ProductSubcategory
g.GeographyKey, DimGeography[CityName], Name] ),
g.CityName, DimGeography[RegionCountryName] ), DimProduct[ProductName]
g.RegionCountryName, SELECTCOLUMNS ( DimSalesTerritory, )
st.SalesTerritoryCountry "GeographyKey", CONVERT ( RETURN
DimSalesTerritory[GeographyKey],
FROM INTEGER ), COUNTX (

DimGeography g DimSalesTerritory[SalesTerritoryCo SUMMARIZE ( _Product,


untry] DimProduct[ProductName] ),
LEFT OUTER JOIN
) DimProduct[ProductName]
DimSalesTerritory st ON
g.GeographyKey = st.GeographyKey; ) )
SQL to DAX Query to DAX Measure #6

SQL Query DAX Query DAX Measure

Combine two tables while removing 17. Combine two tables while removing
17.
duplicates duplicates
SELECT EVALUATE
RegionCountryName AS Country DISTINCT (
FROM UNION (
DimGeography SELECTCOLUMNS(
DimGeography, "Country",
UNION DimGeography[RegionCountryName] ),
SELECT SELECTCOLUMNS(
SalesTerritoryCountry AS Country DimSalesTerritory, "Country",
DimSalesTerritory[SalesTerritoryCo Here's another example of how a DAX
FROM untry] ) query can be converted into a
DimSalesTerritory; ) measure. In this example, we create
a list of countries that are not
) present in the DimSalesTerritory
Show results that appear in the table.
18. first table, but not in the second
SELECT 18. Show results that appear in the
first table, but not in the second
. List of Countries not Present in
DimSalesTerritory Table =
RegionCountryName EVALUATE VAR _Country =
DISTINCT (
FROM DISTINCT ( EXCEPT (
DimGeography EXCEPT ( VALUES (
DimGeography[RegionCountryName] ),
EXCEPT VALUES ( VALUES (
SELECT DimGeography[RegionCountryName] ), DimSalesTerritory[SalesTerritoryCou
VALUES ( ntry] )
SalesTerritoryCountry
DimSalesTerritory[SalesTerritoryCo )
FROM untry] ) )
RETURN
DimSalesTerritory; ) CONCATENATEX ( _Country,
) [RegionCountryName], ", " )

#Bilal
SQL to DAX Query to DAX Measure #7

SQL Query DAX Query DAX Measure

19. Group by a column and perform an


aggregation
19. Group by a column and perform an
aggregation
. A measure in Power BI is a dynamic
calculation whose result depends on
EVALUATE the current filter context. The
SELECT
dimension values in a visual act as
p.BrandName, GROUPBY ( filters. Power BI breaks down the
FactSales, measure calculation based on the
SUM(s.SalesAmount) AS
dimension values. For each unique
Total_Sales DimProduct[BrandName], value of the dimension (e.g., each
FROM FactSales s "TotalSales", SUMX ( brand), the measure is recalculated
CURRENTGROUP (), within that subset of data.
JOIN DimProduct p ON s.ProductKey
= p.ProductKey FactSales[SalesAmount] ) In our case, we want to group the
) total sales by each `BrandName`.
GROUP BY p.BrandName;
We can create the following simple
measure and plot against the
`BrandName`.

Total Sales =
SUM ( FactSales[SalesAmount] )

In visual, Power BI filters the


`FactSales` table to include only
the rows where Brand matches the
current row in the visual and
`SUM(FactSales[SalesAmount])`
calculation is performed on this
filtered subset.

#Bilal

You might also like