Skip to main content

easyfabric.dax_generator

DAX query generation from natural language questions.

Hybrid approach: rule-based pattern matching with LLM fallback.

logging

re

Optional

PowerBIModel

Model

AggregationType

AmbiguousQueryError

ColumnMetadata

InvalidQueryError

MeasureMetadata

ModelContext

QueryIntent

RelationshipMetadata

logger

extract_model_context

def extract_model_context(
model: Model,
power_bi_model: Optional[PowerBIModel] = None) -> ModelContext

Extract metadata from a Model instance into ModelContext for DAX generation.

Arguments:

  • model - Analytical Model with table/column definitions
  • power_bi_model - Optional Power BI model configuration

Returns:

ModelContext with extracted tables, columns, measures, and relationships

RuleBasedParser Objects

class RuleBasedParser()

Parse natural language questions using rule-based patterns.

AGGREGATION_KEYWORDS

TOP_KEYWORDS

BOTTOM_KEYWORDS

__init__

def __init__(context: ModelContext)

parse

def parse(question: str) -> QueryIntent

Parse a natural language question into QueryIntent.

Arguments:

  • question - Natural language question

Returns:

QueryIntent with extracted elements

Raises:

  • AmbiguousQueryError - If multiple entities match without clear intent
  • InvalidQueryError - If question cannot be parsed

build_llm_prompt

def build_llm_prompt(question: str, context: ModelContext) -> str

Build a prompt for Claude API to generate DAX queries.

Arguments:

  • question - Natural language question
  • context - Model context with tables, columns, measures

Returns:

Formatted prompt for Claude

generate_dax_from_llm

def generate_dax_from_llm(question: str, context: ModelContext) -> str

Generate DAX query using Claude API.

Arguments:

  • question - Natural language question
  • context - Model context with tables, columns, measures

Returns:

DAX query string

Raises:

  • InvalidQueryError - If Claude cannot generate valid DAX

generate_dax_from_rule_based

def generate_dax_from_rule_based(intent: QueryIntent,
context: ModelContext) -> str

Generate DAX query from a QueryIntent (rule-based output).

Arguments:

  • intent - Parsed query intent
  • context - Model context with metadata

Returns:

DAX query string

Raises:

  • InvalidQueryError - If query cannot be generated from intent

generate_dax_from_question

def generate_dax_from_question(question: str,
model: Model,
power_bi_model: Optional[PowerBIModel] = None,
use_llm_fallback: bool = True) -> str

Generate DAX query from a natural language question using hybrid approach.

Arguments:

  • question - Natural language query (e.g., "Give me top 10 Cities by Revenue")
  • model - Analytical Model with table/column metadata
  • power_bi_model - Optional Power BI model configuration
  • use_llm_fallback - Whether to use LLM for low-confidence queries

Returns:

DAX query string

Raises:

  • AmbiguousQueryError - If question maps to multiple entities without clear intent
  • InvalidQueryError - If question cannot be parsed into valid DAX