SQL Skills in Power BI
SQL Skills in Power BI
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.
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
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";
)
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
#Bilal
#Bilal
SQL to DAX Query to DAX Measure #5
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
Total Sales =
SUM ( FactSales[SalesAmount] )
#Bilal