dipsikha25 commited on
Commit
dc7fadb
Β·
verified Β·
1 Parent(s): a3b76f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -30
app.py CHANGED
@@ -26,46 +26,70 @@ def ensure_package(package_name: str):
26
  # =========================================================
27
  # βœ… DAX β†’ SQL SUPPORT (NEW)
28
  # =========================================================
29
- LAST_DAX_FORMULA = ""
 
 
 
 
30
 
31
  def convert_dax_to_sql(dax_formula: str):
 
32
  if not dax_formula:
33
  return "No DAX formula available"
34
 
35
- dax = dax_formula.strip()
36
  dax_upper = dax.upper()
37
 
38
- # DISTINCTCOUNT β†’ Snowflake
39
- m = re.search(r"DISTINCTCOUNT\s*\((.*?)\)", dax_upper)
40
- if m:
41
- col = m.group(1)
42
- return f"SELECT COUNT(DISTINCT {col})\nFROM YOUR_TABLE"
43
 
44
- # SUM
45
- m = re.search(r"SUM\s*\((.*?)\)", dax_upper)
 
46
  if m:
47
- col = m.group(1)
48
- return f"SELECT SUM({col})\nFROM YOUR_TABLE"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- # AVERAGE
51
- m = re.search(r"AVERAGE\s*\((.*?)\)", dax_upper)
52
- if m:
53
- col = m.group(1)
54
- return f"SELECT AVG({col})\nFROM YOUR_TABLE"
55
-
56
- # DIVIDE β†’ Snowflake
57
- if "DIVIDE" in dax_upper:
58
- return """SELECT
59
- (numerator / NULLIF(denominator, 0))
60
- FROM YOUR_TABLE"""
61
-
62
- # CALCULATE β†’ WHERE clause
63
- if "CALCULATE" in dax_upper:
64
- return """SELECT AGG_FUNCTION
65
- FROM YOUR_TABLE
66
- WHERE <filters>"""
67
-
68
- return "⚠️ Complex DAX detected β€” enhance parser for full conversion"
69
 
70
 
71
  def convert_to_sql(dax_input):
 
26
  # =========================================================
27
  # βœ… DAX β†’ SQL SUPPORT (NEW)
28
  # =========================================================
29
+ def clean_column(col):
30
+ col = col.replace("'", "")
31
+ col = col.replace("[", ".")
32
+ col = col.replace("]", "")
33
+ return col.strip()
34
 
35
  def convert_dax_to_sql(dax_formula: str):
36
+
37
  if not dax_formula:
38
  return "No DAX formula available"
39
 
40
+ dax = dax_formula.replace("\n", " ")
41
  dax_upper = dax.upper()
42
 
43
+ # βœ… Extract main table
44
+ table_match = re.search(r"'(.*?)'\[", dax)
45
+ main_table = table_match.group(1) if table_match else "YOUR_TABLE"
 
 
46
 
47
+ # βœ… DISTINCTCOUNT
48
+ col = None
49
+ m = re.search(r"DISTINCTCOUNT\s*\((.*?)\)", dax, re.IGNORECASE)
50
  if m:
51
+ col = clean_column(m.group(1))
52
+
53
+ # βœ… Build SELECT
54
+ select_part = f"COUNT(DISTINCT {col})" if col else "AGG_FUNCTION"
55
+
56
+ # βœ… Extract filters (basic logic)
57
+ filters = []
58
+
59
+ # INTERACTED = 1
60
+ m = re.findall(r"'(.*?)'\[(.*?)\]\s*=\s*(\d+)", dax)
61
+ for t, c, v in m:
62
+ filters.append(f"{t}.{c} = {v}")
63
+
64
+ # TEXT filters
65
+ m = re.findall(r"'(.*?)'\[(.*?)\]\s*=\s*\"(.*?)\"", dax)
66
+ for t, c, v in m:
67
+ filters.append(f"{t}.{c} = '{v}'")
68
+
69
+ # ISBLANK β†’ IS NULL
70
+ m = re.findall(r"ISBLANK\s*\((.*?)\)", dax, re.IGNORECASE)
71
+ for expr in m:
72
+ expr = clean_column(expr)
73
+ filters.append(f"{expr} IS NULL")
74
+
75
+ # OR conditions (basic support)
76
+ if "||" in dax:
77
+ filters.append("(OR condition detected β€” refine manually)")
78
+
79
+ # βœ… WHERE clause
80
+ where_clause = ""
81
+ if filters:
82
+ where_clause = "\nWHERE " + "\n AND ".join(filters)
83
+
84
+ # βœ… FINAL SQL
85
+ sql = f"""
86
+ SELECT {select_part}
87
+ FROM {main_table}
88
+ {where_clause}
89
+ """.strip()
90
+
91
+ return sql
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
 
95
  def convert_to_sql(dax_input):