DEV Community

JW SOLUTIONS
JW SOLUTIONS

Posted on Originally published at jw-solutions.com

SQL and DAX: three habits for fast, readable queries

Tip of the week

In SQL, select only the columns you need and filter early. In DAX, store intermediate results in variables (VAR) so they are evaluated once.

SELECT o.id, o.total, c.name
FROM orders AS o
JOIN customers AS c ON c.id = o.customer_id
WHERE o.created_at >= DATE '2026-01-01'
ORDER BY o.total DESC;
Enter fullscreen mode Exit fullscreen mode
Ventas YTD Margen =
VAR Ventas = [Ventas Totales]
VAR Costo = [Costo Total]
RETURN DIVIDE(Ventas - Costo, Ventas)
Enter fullscreen mode Exit fullscreen mode

Quick rules

  • Avoid SELECT * in production.
  • Use DIVIDE in DAX to avoid division-by-zero errors.
  • One measure, one responsibility: compose measures instead of duplicating logic.

Try it interactively: Format, validate, and auto-document your sql directly in JW Solutions Code Formatter & Auto-Doc AI.
Originally published at jw-solutions.com.

Top comments (0)