Skip to content

Commit

Permalink
Add more macros. Now we have a helpers folder to modularize functions…
Browse files Browse the repository at this point in the history
… and an excel folder for the macros that resemble excel formulas.
  • Loading branch information
jmperafan authored Mar 2, 2023
1 parent fe4c155 commit b558819
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 53 deletions.
48 changes: 0 additions & 48 deletions dbt_project/macros/averagea.sql

This file was deleted.

32 changes: 32 additions & 0 deletions dbt_project/macros/excel/averagea.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{#
Returns the average (arithmetic mean) of the values in a column.
Handles text and non-numeric values.
When is this a good idea? Not sure. Lol.

Example:

Input: `Table`

| example |
|---------|
| 10 |
| 10s |
| 30 |
| True |

select
{{ averagea('example') }}
from public.test

Output:
10.25

Arguments:
column: Column name, required
#}

{% macro averagea(column) %}

avg({{ reckless_casting(column) }})

{% endmacro %}
31 changes: 31 additions & 0 deletions dbt_project/macros/excel/averageif.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{#
Calculates the average of values in a range based on a true or false condition.

Example:

Input: 'B2:B4', 'Product A', 'A2:A4'

| Quantity | Product |
|----------|-----------|
| 2 | Product A |
| 4 | Product A |
| 3 | Product B |

select {{ averageif('B2:B4', 'Product A', 'A2:A4') }}

Output:
3

Arguments:
range: Range of cells that you want evaluated by criteria, required.
criteria: Text that defines which cells will be added, required.
sum_range: The actual cells to aggregate, required.
#}

{% macro averageif(range, criteria, average_range) %}
(
select avg({{ average_range }})
from {{ table }}
where range = criteria
)
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ Example:
| 1 | 20 |
| 3 | 5 |

select {{ averagex('example') }}

select {{ averagex('Orders', 'Quantity * Sales') }}

Output:

18.33


Arguments:
table: The table containing the rows for which the expression will be evaluated, required.
expression: The expression to be evaluated for each row of the table, required.
Expand All @@ -29,4 +26,4 @@ Arguments:
select avg({{ expression }})
from {{ table }}
)
{% endmacro %}
{% endmacro %}
29 changes: 29 additions & 0 deletions dbt_project/macros/excel/sumx.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{#
Returns the sum of an expression evaluated for each row in a table.

Example:

Input: 'Orders', 'Quantity * Sales'

| Quantity | Sales |
|----------|--------|
| 2 | 10 |
| 1 | 20 |
| 3 | 5 |

select {{ sumx('Orders', 'Quantity * Sales') }}

Output:
55

Arguments:
table: The table containing the rows for which the expression will be evaluated, required.
expression: The expression to be evaluated for each row of the table, required.
#}

{% macro sumx(table, expression) %}
(
select sum({{ expression }})
from {{ table }}
)
{% endmacro %}
5 changes: 5 additions & 0 deletions dbt_project/macros/helpers/enforce_string.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% macro enforce_string(column) %}

lower(cast({{ column }} as text))

{% endmacro %}
11 changes: 11 additions & 0 deletions dbt_project/macros/helpers/get_column_names.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{%- macro get_column_names(relation) -%}

{%- set columns = adapter.get_columns_in_relation(relation) -%}
{%- set columns_list = [] -%}

{%- for column in columns -%}
{{ columns_list.append(columns_list.name) }}
{%- endfor -%}
{{ return(columns_list) }}

{%- endmacro -%}
5 changes: 5 additions & 0 deletions dbt_project/macros/helpers/letters_to_numbers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% macro letters_to_numbers(letter) %}

ascii(upper(letter)) - 64

{% endmacro %}
17 changes: 17 additions & 0 deletions dbt_project/macros/helpers/reckless_casting.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% macro reckless_casting(column) %}

{% set match_positive_boolean = ('y', 'yes', 't', 'true') %}
{% set match_negative_boolean = ('n', 'no', 'f', 'false') %}
{% set integer_regex = '^([0-9]+)$' %}
{% set float_regex = '^[-+]?[0-9]*\.[0-9]+([eE][-+]?[0-9]+)?$' %}
{% set text_regex = '[^0-9.]' %}

case
when {{ enforce_string(column) }} in {{ match_positive_boolean }} then 1.0
when {{ enforce_string(column) }} in {{ match_negative_boolean }} then 0.0
when regexp_matches({{ enforce_string(column) }}, '{{ integer_regex }}') then cast({{ column }} as real)
when regexp_matches({{ enforce_string(column) }}, '{{ float_regex }}') then cast({{ column }} as real)
when regexp_matches({{ enforce_string(column) }}, '{{ text_regex }}') then 0.0
end

{% endmacro %}

0 comments on commit b558819

Please sign in to comment.