ID 11 - Filter using where condition limit

Max: 45
Try code in DAX.do

Syntax

FILTER SELECTCOLUMNS TOPN YEAR

Related

37 - Ranks customers by total sales grouped product 14 - Filter using where condition ORDER BY 36 - Top 10 Customers by Total Sales with Ranking 31 - Calculate the Average Sales Amount for Each Customer with Sales Above a Certain Threshold

SQL

SELECT customerName, Yearly_Income 
FROM customers 
WHERE Yearly_Income BETWEEN 21000 AND 100000 
LIMIT 5;

DAX

EVALUATE
TOPN(
    5,
    SELECTCOLUMNS(
        FILTER(
            Customer,
            Customer[Yearly Income] >= 21000 && Customers[Yearly Income] <= 100000
        ),
        "CustomerName", Customer[Name],
        "CreditLimit", Customer[Yearly Income]
    )
)

Pandas

import pandas as pd

df = pd.read_csv('customers.csv') 

# Filter the DataFrame to include rows where Yearly Income is between 21,000 and 100,000
result = df[(df['Yearly Income'] >= 21000) & (df['Yearly Income'] <= 100000)][['customerName', 'creditLimit']]

# Limit the results to 5 rows
result = result.head(5)

# Convert to a list of lists
result_list = result.values.tolist()

print(result_list)

Output

John Doe 45000
Jane Smith 22000
Bob Brown 80000
Chris Green 25000

Explanation

FILTER(Customers, Customers[Yearly Income] >= 21000 && Customers[Yearly Income] <= 100000): Filters the Customers table to include rows where the Yearly Income is between 21,000 and 100,000. SELECTCOLUMNS: Selects the customerName and creditLimit columns and returns them as "CustomerName" and "Yearly Income" in the output. TOPN(5, ...): Limits the number of rows returned to 5

Leave a Comment